11-16 06:24:05.968 11496-11496/? D/myLogs: --- onCreate database ---
11-16 06:24:06.004 11496-11496/? D/AndroidRuntime: Shutting down VM
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: FATAL EXCEPTION: main
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: Process: com.example.prog, PID: 11496
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: java.lang.IllegalStateException: getDatabase called recursively
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:203)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at com.example.prog.MainActivity$DBHelper.onCreate(MainActivity.java:483)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at com.example.prog.MainActivity.onClick(MainActivity.java:406)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.view.View.performClick(View.java:4756)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:19749)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5221)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-16 06:24:06.009 11496-11496/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
代码在这里
case R.id.button10:
DBHelper dbHelper = new DBHelper(this);
ContentValues cv = new ContentValues();
SQLiteDatabase db = dbHelper.getWritableDatabase();
String temperature = editText5.getText().toString();
String percent = editText7.getText().toString();
if (temperature.equalsIgnoreCase("")) {
break;
}
if (percent.equalsIgnoreCase("")) {
break;
}
Log.d(LOG_TAG, "DB search");
Cursor c1 = db.query("mytable", null, "temperature, percent = ?, ?", new String[]{temperature, percent}, null, null, null);
if (c1.moveToFirst()) {
int ckoef = c1.getColumnIndex("c");
int density = c1.getColumnIndex("density");
do {
textView9.setText(c1.getString(ckoef));
textView11.setText(c1.getString(density));
} while (c1.moveToNext());
} else
textView9.setText("oops");
textView11.setText("no matches");
c1.close();
dbHelper.close();
break;
和班级
class DBHelper extends SQLiteOpenHelper {
public DBHelper(Context context) {
super(context, "myDB", null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db) {
int percent[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
int temperature[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
double ckoef [] = {4.025, 4.034, 4.043, 4.057, 4.075, 4.085, 4.094, 4.103, 4.113, 4.122, 4.132, 3.87, 3.885, 3.9, 3.915, 3.93, 3.945, 3.96, 3.975, 3.99, 4.005, 4.02, 3.966, 3.977, 3.988, 3.999, 4.010, 4.022, 4.034, 4.046, 4.058, 4.069, 4.081, 3.436, 3.46, 3.5, 3.53, 3.56, 3.595, 3.62, 3.65, 3.68, 3.715, 3.74, 3.21, 3.25, 3.28, 3.32, 3.36, 3.399, 3.433, 3.472, 3.511, 3.546, 3.850 };
double density [] = {1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1036, 1033, 1030, 1026, 1022, 1018, 1014, 1010, 1006, 1001.5, 997, 1027.852, 1025.148, 1018.85, 1015.23, 1011.63, 1008, 1004.37, 1000.741, 996.61, 992.482, 1072.667, 1068.333, 1064, 1058.167, 1052.167, 1046.667, 1041, 1035.333, 1029.667, 1023.5, 1017.333, 1089.852, 1084.926, 1080, 1073.57, 1067.148, 1061.185, 1055.222, 1049.259, 1043.296, 1036.833, 1030.37};
Log.d(LOG_TAG, "--- onCreate database ---");
// создаем таблицу с полями
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "percent integer,"
+ "temperature integer," + "ckoef double" + "density double" + ");");
db = getWritableDatabase();
Cursor c = db.query("mytable", null, null, null, null, null, null);
ContentValues cv = new ContentValues();
for (int i = 0; i < 55; i++) {
cv.put("percent", percent[i]);
cv.put("temperature", temperature[i]);
cv.put("c", ckoef[i]);
cv.put("density", density[i]);
Log.d(LOG_TAG, "id = " + db.insert("mytable", null, cv));
}
db.close();
Log.d(LOG_TAG, "все данные");
c = db.query("mytable", null, null, null, null, null, null);
c.close();
// эмулируем нажатие кнопки btnAll
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
if (oldVersion == 1 && newVersion == 2) {
db.delete("mytable", null, null);
Log.d(LOG_TAG, "--- onUpgrade database ---");
ContentValues cv = new ContentValues();
// данные для таблицы должностей
int percent[] = {10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30,
40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50};
int temperature[] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100,
0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
double ckoef [] = {4.025, 4.034, 4.043, 4.057, 4.075, 4.085, 4.094, 4.103, 4.113, 4.122, 4.132, 3.87, 3.885, 3.9, 3.915, 3.93, 3.945, 3.96, 3.975, 3.99, 4.005, 4.02,
3.966, 3.977, 3.988, 3.999, 4.010, 4.022, 4.034, 4.046, 4.058, 4.069, 4.081, 3.436, 3.46, 3.5, 3.53, 3.56, 3.595, 3.62, 3.65, 3.68, 3.715, 3.74, 3.21, 3.25, 3.28,
3.32, 3.36, 3.399, 3.433, 3.472, 3.511, 3.546, 3.850 };
double density [] = {1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1012.5, 1036, 1033, 1030, 1026, 1022, 1018, 1014, 1010, 1006, 1001.5,
997, 1027.852, 1025.148, 1018.85, 1015.23, 1011.63, 1008,
1004.37, 1000.741, 996.61, 992.482, 1072.667, 1068.333, 1064, 1058.167, 1052.167, 1046.667, 1041, 1035.333, 1029.667, 1023.5, 1017.333, 1089.852, 1084.926, 1080,
1073.57, 1067.148, 1061.185, 1055.222, 1049.259, 1043.296, 1036.833, 1030.37};
db.beginTransaction();
try {
// создаем таблицу должностей
db.execSQL("create table mytable ("
+ "id integer primary key autoincrement," + "percent integer,"
+ "temperature integer," + "ckoef double" + "density double" + ");");
// заполняем ее
for (int i = 0; i < 55; i++) {
cv.put("percent", percent[i]);
cv.put("temperature", temperature[i]);
cv.put("c", ckoef[i]);
cv.put("density", density[i]);
Log.d(LOG_TAG, "id = " + db.insert("mytable", null, cv));
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
}
请帮助
答案 0 :(得分:2)
请勿在{{1}} getWritableDatabase()
回调中致电SQLiteOpenHelper
。相反,只需使用传递给你的onCreate()
作为参数。