我有一个带有两个表的 SQLite 数据库,一个用于保存登录信息,例如电子邮件地址和密码,另一个用于保存用户的 GPS 坐标。我在 DBadapter 类“DatabaseHandler.class”的 OnCreate 方法中创建了两个表。登录表工作正常,但坐标表不能。当我尝试访问并在其上运行命令时,程序会抛出“不存在这样的表”。
日志猫
01-22 12:42:06.769: E/AndroidRuntime(854): FATAL EXCEPTION: main
01-22 12:42:06.769: E/AndroidRuntime(854): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidbasic12/com.example.androidbasic12.map}: android.database.sqlite.SQLiteException: no such table: coord (code 1): , while compiling: SELECT * FROM coord WHERE name = name
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.os.Looper.loop(Looper.java:137)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-22 12:42:06.769: E/AndroidRuntime(854): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 12:42:06.769: E/AndroidRuntime(854): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 12:42:06.769: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-22 12:42:06.769: E/AndroidRuntime(854): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-22 12:42:06.769: E/AndroidRuntime(854): at dalvik.system.NativeStart.main(Native Method)
01-22 12:42:06.769: E/AndroidRuntime(854): Caused by: android.database.sqlite.SQLiteException: no such table: coord (code 1): , while compiling: SELECT * FROM coord WHERE name = name
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
01-22 12:42:06.769: E/AndroidRuntime(854): at com.example.androidbasic12.library.DatabaseHandler.getLatLng(DatabaseHandler.java:155)
01-22 12:42:06.769: E/AndroidRuntime(854): at com.example.androidbasic12.library.UserFunctions.getCoords(UserFunctions.java:124)
01-22 12:42:06.769: E/AndroidRuntime(854): at com.example.androidbasic12.map.onCreate(map.java:56)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.Activity.performCreate(Activity.java:5104)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-22 12:42:06.769: E/AndroidRuntime(854): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-22 12:42:06.769: E/AndroidRuntime(854): ... 11 more
DatabaseHandler.class
public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "android_api";
//Carcoord table name
private static final String TABLE_CARCOORD = "coord";
// carcoord table columns names
private static final String CAR_KEY_ID = "id";
private static final String CAR_KEY_NAME = "name";
private static final String CAR_KEY_LAT = "lat";
private static final String CAR_KEY_LNG = "lng";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_UID = "uid";
private static final String KEY_CREATED_AT = "created_at";
//Strings that handles create table querys
private static final String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE,"
+ KEY_UID + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
private static final String CREATE_CARCOORD_TABLE = "CREATE TABLE " + TABLE_CARCOORD + "("
+ CAR_KEY_ID + " INTEGER PRIMARY KEY,"
+ CAR_KEY_NAME + " TEXT,"
+ CAR_KEY_LAT + " TEXT,"
+ CAR_KEY_LNG + " TEXT" + ")";
//Constructor
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_LOGIN_TABLE);
db.execSQL(CREATE_CARCOORD_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CARCOORD);
// Create tables again
onCreate(db);
}
/**
* Store coordinates in TABLEE_COORDS
*/
public void addCoord(String name, String lat, String lng){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(CAR_KEY_LAT, lat); // latitude
values.put(CAR_KEY_LNG, lng); //Longitude
// Inserting row
db.insert(TABLE_CARCOORD, null, values);
db.close(); // Closing writable database connection
}
/**
* Storing user details in database
* */
public void addUser(String name, String email, String uid, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, name); // Name
values.put(KEY_EMAIL, email); // Email
values.put(KEY_UID, uid); // Email
values.put(KEY_CREATED_AT, created_at); // Created At
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}
/**
* Getting user data from database
* */
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String,String>();
String selectQuery = "SELECT * FROM " + TABLE_LOGIN;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// Move to first row
cursor.moveToFirst();
if(cursor.getCount() > 0){
user.put("name", cursor.getString(1));
user.put("email", cursor.getString(2));
user.put("uid", cursor.getString(3));
user.put("created_at", cursor.getString(4));
}
cursor.close();
db.close();
// return user
return user;
}
/**
* Getting user login status
* return true if rows are there in table
* */
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;
}
public Cursor getUsername(){
SQLiteDatabase db = this.getReadableDatabase();
String query = ("SELECT * FROM " + TABLE_LOGIN + " WHERE name = " + KEY_NAME);
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
public Cursor getLatLng(){
SQLiteDatabase db = this.getReadableDatabase();
String query = ("SELECT * FROM " + TABLE_CARCOORD + " WHERE name = " + CAR_KEY_NAME);
Cursor cursor = db.rawQuery(query, null);
return cursor;
}
/**
* reset table TABLE_CARCOORD
*/
public void resetCarcoord(){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CARCOORD, null, null);
db.close();
}
/**
* Re create 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();
}
}
来自 UserFunctions.class 的代码片段,我在其中使用表“coord”。
/**
* Get lat and lng from TABLE_CARCOORDS
*/
public ArrayList<String> getCoords(Context context, String lat, String lng) {
DatabaseHandler db = new DatabaseHandler(context);
Cursor cursor = db.getLatLng();
ArrayList<String> cordarr = new ArrayList<String>();
if (cursor.moveToFirst()){
lat = cursor.getString(cursor.getColumnIndex("lat"));
lng = cursor.getString(cursor.getColumnIndex("lng"));
cordarr.add(lat);
cordarr.add(lng);
}
cursor.close();
db.close();
return cordarr;
}
修复后的 LogCats
第一个问题
01-22 14:15:07.080: E/SQLiteDatabase(20980): close() was never explicitly called on database '/data/data/com.example.androidbasic12/databases/android_api'
01-22 14:15:07.080: E/SQLiteDatabase(20980): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteDatabase.<init>(SQLiteDatabase.java:2072)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1126)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1083)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:1170)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:844)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:228)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:157)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at com.example.androidbasic12.library.DatabaseHandler.getUsername(DatabaseHandler.java:146)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at com.example.androidbasic12.library.UserFunctions.getUname(UserFunctions.java:87)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at com.example.androidbasic12.DashboardActivity.onCreate(DashboardActivity.java:59)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.Activity.performCreate(Activity.java:4470)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.os.Looper.loop(Looper.java:137)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-22 14:15:07.080: E/SQLiteDatabase(20980): at dalvik.system.NativeStart.main(Native Method)
01-22 14:15:07.080: E/System(20980): Uncaught exception thrown by finalizer
01-22 14:15:07.085: E/System(20980): java.lang.IllegalStateException: Don't have database lock!
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase.verifyLockOwner(SQLiteDatabase.java:2230)
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2322)
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase$1.entryRemoved(SQLiteDatabase.java:2318)
01-22 14:15:07.085: E/System(20980): at android.util.LruCache.trimToSize(LruCache.java:197)
01-22 14:15:07.085: E/System(20980): at android.util.LruCache.evictAll(LruCache.java:285)
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase.deallocCachedSqlStatements(SQLiteDatabase.java:2283)
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase.closeClosable(SQLiteDatabase.java:1255)
01-22 14:15:07.085: E/System(20980): at android.database.sqlite.SQLiteDatabase.finalize(SQLiteDatabase.java:2043)
01-22 14:15:07.085: E/System(20980): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:185)
01-22 14:15:07.085: E/System(20980): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:168)
01-22 14:15:07.085: E/System(20980): at java.lang.Thread.run(Thread.java:856)
第二个错误
01-22 14:15:07.435: E/AndroidRuntime(20980): FATAL EXCEPTION: main
01-22 14:15:07.435: E/AndroidRuntime(20980): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidbasic12/com.example.androidbasic12.map}: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread.access$600(ActivityThread.java:127)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.os.Handler.dispatchMessage(Handler.java:99)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.os.Looper.loop(Looper.java:137)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread.main(ActivityThread.java:4511)
01-22 14:15:07.435: E/AndroidRuntime(20980): at java.lang.reflect.Method.invokeNative(Native Method)
01-22 14:15:07.435: E/AndroidRuntime(20980): at java.lang.reflect.Method.invoke(Method.java:511)
01-22 14:15:07.435: E/AndroidRuntime(20980): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
01-22 14:15:07.435: E/AndroidRuntime(20980): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
01-22 14:15:07.435: E/AndroidRuntime(20980): at dalvik.system.NativeStart.main(Native Method)
01-22 14:15:07.435: E/AndroidRuntime(20980): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
01-22 14:15:07.435: E/AndroidRuntime(20980): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
01-22 14:15:07.435: E/AndroidRuntime(20980): at java.util.ArrayList.get(ArrayList.java:304)
01-22 14:15:07.435: E/AndroidRuntime(20980): at com.example.androidbasic12.map.onCreate(map.java:57)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.Activity.performCreate(Activity.java:4470)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
01-22 14:15:07.435: E/AndroidRuntime(20980): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
01-22 14:15:07.435: E/AndroidRuntime(20980): ... 11 more