我在尝试在android中建立数据库时遇到问题,每次我登录时,日志猫都会说没有这样的表。
public class DatabaseOnline extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "name";
private static final String TABLE_LOGIN = "online";
private static final String KEY_ID = "Registration_id";
private static final String KEY_NAME = "Name";
private static final String KEY_PASSWORD = "Password";
private static final String KEY_UNIQUEID = "unique_id";
public DatabaseOnline(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_NAME + " VARCHAR (255), " + KEY_PASSWORD
+ " VARCHAR (255), " + KEY_ID + " VARCHAR(255), "
+ KEY_UNIQUEID + " VARCHAR (255)" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}
public void addUser(String name, String password, String regId,
String unique_id) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_PASSWORD, password); // Email
values.put(KEY_ID, regId);
values.put(KEY_UNIQUEID, unique_id);
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}
public int getRowCount() {
String countQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
int rowCount = cursor.getCount();
db.close();
cursor.close();
// return row count
return rowCount;
}
/**
* Re crate database Delete all tables and create them again
* */
public void resetTables() {
SQLiteDatabase db = this.getWritableDatabase();
// Delete All Rows
db.delete(TABLE_LOGIN, null, null);
db.close();
}
}
问题是当我登录时:
DatabaseOnline db = new DatabaseOnline(getApplicationContext());
db.getWritableDatabase();
JSONObject json_user = json.getJSONObject("user");
System.out.println("unique id " + json_user.getString("unique_id"));
System.out.println("password " + json_user.getString("password"));
db.addUser(json_user.getString("name"),json_user.getString("password"),json_user.getString("regId"),json_user.getString("unique_id"));
Intent dashboard = new Intent(getApplicationContext(),Main.class);
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
finish();
出现的是android没有创建数据库。这是日志猫:
11-20 21:42:15.162: E/SQLiteLog(1176): (1) no such table: online
11-20 21:42:15.282: E/SQLiteDatabase(1176): Error inserting unique_id=50a939dc9fa8a4.09780382 Name=v Registration_id=APA91bHnScWAF0xjtMjyHTvHx3ON6Eu_8on6kZUL5EvDmtAK8WDxwBVpNyMoapaTl0-ProAnY0m9VZbS-0OPeMshQkeU7ZOHlfI8tUAVJbeIgoe9sdL061ntJ8MnyglrLHK2BOWH2K1n Password=9e3669d19b675bd57058fd4664205d2a
11-20 21:42:15.282: E/SQLiteDatabase(1176): android.database.sqlite.SQLiteException: no such table: online (code 1): , while compiling: INSERT INTO online(unique_id,Name,Registration_id,Password) VALUES (?,?,?,?)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at guc.edu.i.DatabaseOnline.addUser(DatabaseOnline.java:66)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at guc.edu.i.Login.login(Login.java:199)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at guc.edu.i.Login$7.run(Login.java:146)
11-20 21:42:15.282: E/SQLiteDatabase(1176): at java.lang.Thread.run(Thread.java:856)