کد:
package aliluncher.prj;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.Calendar;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "MyTestDB.db";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
SQLiteDatabase sqLiteDatabase = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL("CREATE TABLE USERS_TABLE(\n" +
" PUserID INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
" PUserNAME TEXT NOT NULL,\n" +
" PUserPass TEXT NOT NULL,\n" +
" InsDateTime DATETIME NOT NULL,\n" +
" UserRanking DECIMAL NOT NULL,\n" +
" UserAverage FLOAT NOT NULL\n" +
");");
//UDF_InsertDataToDB(sqLiteDatabase);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void UDF_InsertDataToDB(double v , float v1)
{
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
String mydate = java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime());
sqLiteDatabase.execSQL("INSERT INTO USERS_TABLE (PUserNAME , PUserPass , InsDateTime , UserRanking , UserAverage)" +
" VALUES ('ALI' , 'ALI3' , '" + mydate + "' , " + v +" , " + v1 +");");
}
}
کد:
package aliluncher.prj;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
myDB = new DatabaseHelper(this);
for(int i = 0; i<= 2000; i++) {
myDB.UDF_InsertDataToDB(i/7 ,(float)java.lang.Math.cos(i) );
}
} catch (Exception e) {
TextView tx = (TextView)findViewById(R.id.link_signup);
tx.setText(e.getMessage());
}
}
}