我可以在我的数据库中插入信息,但是当我想在阅读它们之后阅读它们就像developer.android一样,它会接近这个错误“不幸的是,app已停止”。
这是我的代码:
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getApplicationContext());
SQLiteDatabase db = mDbHelper.getWritableDatabase();
ContentValues values = new ContentValues(); //for insert
Date now=new Date(); //for insert
values.put("ID", 1); //for insert
values.put("kalame", "good"); //for insert
values.put("mani", "salam"); //for insert
values.put("marhale", "1"); //for insert
values.put("taghir", String.valueOf(now.getTime())); //for insert
long newRowID=db.insert("english", "taghir" , values); //for insert
String[] meghdarha={"good"};
String[] sotoonha={"ID","kalame","mani","marhale","taghir"};
Cursor nowRead =db.query("english", sotoonha, "kalame = good", null, null, null, null);
nowRead.moveToFirst();
long newWord=nowRead.getLong(nowRead.getColumnIndexOrThrow("ID"));
TextView tv= (TextView)findViewById(R.id.textView1);
tv.setText(String.valueOf(newWord));
这是我的FeedReaderDbHelper:
public class FeedReaderDbHelper extends SQLiteOpenHelper {
// If you change the database schema, you must increment the database version.
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "FeedReader.db";
public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT );";
public static final String pak="DROP TABLE IF EXISTS english";
public FeedReaderDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(sakht);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// This database is only a cache for online data, so its upgrade policy is
// to simply to discard the data and start over
db.execSQL(pak);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
这是我的日志猫:
02-09 18:58:22.762: D/gralloc_goldfish(581): Emulator without GPU emulation detected.
02-09 18:59:51.202: E/SQLiteDatabase(581): Error inserting ID=1 mani=salam taghir=1391959791144 marhale=1 kalame=good
02-09 18:59:51.202: E/SQLiteDatabase(581): android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.database.sqlite.SQLiteStatement.native_executeInsert(Native Method)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:113)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
02-09 18:59:51.202: E/SQLiteDatabase(581): at ir.maghsoodi.leitnerbox.Insert.onCreate(Insert.java:31)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.Activity.performCreate(Activity.java:4465)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.os.Looper.loop(Looper.java:137)
02-09 18:59:51.202: E/SQLiteDatabase(581): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 18:59:51.202: E/SQLiteDatabase(581): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 18:59:51.202: E/SQLiteDatabase(581): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 18:59:51.202: E/SQLiteDatabase(581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 18:59:51.202: E/SQLiteDatabase(581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 18:59:51.202: E/SQLiteDatabase(581): at dalvik.system.NativeStart.main(Native Method)
02-09 18:59:51.232: D/AndroidRuntime(581): Shutting down VM
02-09 18:59:51.242: W/dalvikvm(581): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
02-09 18:59:51.342: E/AndroidRuntime(581): FATAL EXCEPTION: main
02-09 18:59:51.342: E/AndroidRuntime(581): java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.maghsoodi.leitnerbox/ir.maghsoodi.leitnerbox.Insert}: android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of range: handle 0x1ee560
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.os.Looper.loop(Looper.java:137)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread.main(ActivityThread.java:4340)
02-09 18:59:51.342: E/AndroidRuntime(581): at java.lang.reflect.Method.invokeNative(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581): at java.lang.reflect.Method.invoke(Method.java:511)
02-09 18:59:51.342: E/AndroidRuntime(581): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-09 18:59:51.342: E/AndroidRuntime(581): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-09 18:59:51.342: E/AndroidRuntime(581): at dalvik.system.NativeStart.main(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581): Caused by: android.database.sqlite.SQLiteBindOrColumnIndexOutOfRangeException: bind or column index out of range: handle 0x1ee560
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteProgram.native_bind_string(Native Method)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:247)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:299)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:401)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:55)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1564)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1449)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1405)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1485)
02-09 18:59:51.342: E/AndroidRuntime(581): at ir.maghsoodi.leitnerbox.Insert.onCreate(Insert.java:38)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.Activity.performCreate(Activity.java:4465)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-09 18:59:51.342: E/AndroidRuntime(581): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-09 18:59:51.342: E/AndroidRuntime(581): ... 11 more
02-09 19:04:51.563: I/Process(581): Sending signal. PID: 581 SIG: 9
我该如何解决? 请帮帮我
答案 0 :(得分:1)
不确定这是否是问题,但您创建的表查询缺少“);”
你有
public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT";
但它应该是
public static final String sakht="CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT);";
您的查询也是错误的。你可以做到
Cursor nowRead =db.query("english", sotoonha, "kalame = good", null, null, null, null);
或更具活力
Cursor nowRead =db.query("english", sotoonha, "kalame = ?", meghdarha, null, null, null);
查看以下帖子了解详情 How to pass two or more selection argument in "query" method
答案 1 :(得分:0)
“约束失败”与您的主键有关。您正在尝试插入带有重复“ID”的行
创建表格时:
CREATE TABLE english (ID INTEGER PRIMARY KEY,kalame TEXT,mani TEXT,marhale TEXT,taghir TEXT );"
你告诉SQLite列“ID”不能有重复的值。当您重新启动应用程序时,您的数据库已经存在,并且您正在尝试再次插入“ID = 1”的行:
long newRowID=db.insert("english", "taghir" , values); //for insert