我的SQLite包下的所有文件都出错了。由于这些是预定义的类,并且不需要或不允许编辑,因此我很难完成项目。
代码太长,无法在此处发布。 例如,SQLiteDatabase.java中的这些导入显示为红色:
import android.database.sqlite.SQLiteDebug.DbStats;
import dalvik.system.CloseGuard;
我的logcat显示以下错误:
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: FATAL EXCEPTION: main
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: select * from user
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.example.abcd.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:40)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.example.abcd.helloworld.SignUp$1.onClick(SignUp.java:52)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
我所做的所有编码似乎都是正确的。
表创建的代码是:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "user.db";
private static final String TABLE_NAME = "user";
private static final String COLUMN_ID = "id";
private static final String COLUMN_NAME = "name";
private static final String COLUMN_EMAIL = "email";
private static final String COLUMN_UNAME = "uname";
private static final String COLUMN_PASS = "pass";
SQLiteDatabase db;
private static final String TABLE_CREATE = "create table user (id integer primary key autoincrement," +
"name text, email text, uname text, pass text);";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertUser(User u) {
db = this.getWritableDatabase();
ContentValues values = new ContentValues();
String query = "select * from user";
Cursor cursor = db.rawQuery(query, null);
int count = cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_NAME, u.getName());
values.put(COLUMN_EMAIL, u.getEmail());
values.put(COLUMN_UNAME, u.getUname());
values.put(COLUMN_PASS, u.getPass());
db.insert(TABLE_NAME, null, values);
}
public String searchPass(String uname) {
db = this.getReadableDatabase();
String query = "select uname, pass from" + TABLE_NAME;
Cursor cursor = db.rawQuery(query, null);
String a, b;
b = "not found";
if (cursor.moveToFirst()) {
do {
a = cursor.getString(0);
if (a.equals(uname)) {
b = cursor.getString(1);
break;
}
}
while (cursor.moveToNext());
}
db.close();
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
db.execSQL(query);
this.OnCreate(db);
}
private void OnCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
}
答案 0 :(得分:0)
您在代码中使用onCreate方法两次。删除重复。
尝试以下代码:
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "user.db";
public DBHandler(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String query = "create table user(" +
"id integer primary key autoincrement, " +
"name text, "+
"email text, "+
"uname text, "+
"pass text"+
");";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query = "drop table if exists user";
db.execSQL(query);
onCreate(db);
}
/*
Remaining code
Please do not add onCreate and onUpgrade method anymore.
*/
}
答案 1 :(得分:-1)
您的代码似乎没问题,尝试调试SQLite代码。在android代码之外执行语句。打开终端并执行sql语句,然后执行.tables,此命令将显示数据库中现有的表。
更多信息,你可以在这里找到, http://www.tutorialspoint.com/sqlite/sqlite_create_table.htm