我试图在创建表时在 SQLite 数据库中添加值,但代码意外停止。我试图通过在日志中输入命令来检查它在哪里弯腰。两个表都成功创建。当我们尝试在 onCreate() 方法中插入值时,就会出现问题。任何建议,有什么问题?
package moin.WPG.databasecheck;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
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 = "applicationManager";
// Contacts table name
private static final String TABLE_EMERGENCY_CONTACTS = "emerencyContacts";
private static final String TABLE_PERSONAL_CONTACTS = "personalContacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
private static final String KEY_EMAIL_ID = "email_id";
private static final String KEY_USER_ID = "userId";
private static final String KEY_APP_PASSWORD = "app_password";
private static final String KEY_USER_NAME = "user_name";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_EMERGENCY_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EMERGENCY_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT," + KEY_EMAIL_ID + " TEXT" + ")";
db.execSQL(CREATE_EMERGENCY_CONTACTS_TABLE);
Log.d("table","table created 1");
String CREATE_PERSONAL_CONTACTS_TABLE = "CREATE TABLE " + TABLE_PERSONAL_CONTACTS + "("
+ KEY_USER_ID + " INTEGER PRIMARY KEY," + KEY_USER_NAME + " TEXT,"
+ KEY_APP_PASSWORD + " TEXT" + ")";
db.execSQL(CREATE_PERSONAL_CONTACTS_TABLE);
Log.d("table","table created 2");
db = this.getWritableDatabase();
ContentValues[] values = new ContentValues[3];
for(int i=1;i<=3;i++){
values[i-1].put(KEY_ID, i);
values[i-1].put(KEY_NAME, "name"+i); // Contact Name
values[i-1].put(KEY_PH_NO, "ph"+1);// Contact Phone
values[i-1].put(KEY_EMAIL_ID, "id"+i);
db.insert(TABLE_EMERGENCY_CONTACTS, null, values[i-1]);
}
Log.d("inserted","table 1");
ContentValues values2 = new ContentValues();
values2.put(KEY_USER_ID, 1);
values2.put(KEY_USER_NAME, "username");
values2.put(KEY_APP_PASSWORD, "password");
db.insert(TABLE_PERSONAL_CONTACTS, null, values2);
Log.d("inserted","table 2"); */
db.close(); // Closing database connection
Log.d("database","closing");
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS ");
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new contact
/* void addContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, contact.getID());
values.put(KEY_NAME, contact.getName()); // Contact Name
values.put(KEY_PH_NO, contact.getPhoneNumber());// Contact Phone
values.put(KEY_EMAIL_ID, contact.getEmailId());
// Inserting Row
db.insert(TABLE_EMERGENCY_CONTACTS, null, values);
db.close(); // Closing database connection
} */
// Getting single contact
Contact getEmergencyContact(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMERGENCY_CONTACTS, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO, KEY_EMAIL_ID }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Contact contact = new Contact(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),cursor.getString(3));
// return contact
return contact;
}
UserInfo getUserInfo(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_PERSONAL_CONTACTS, new String[] { KEY_USER_ID,
KEY_USER_NAME, KEY_APP_PASSWORD }, KEY_USER_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
UserInfo userInfo = new UserInfo(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
// return contact
return userInfo;
}
// Getting All Contacts
/* public List<Contact> getAllContacts() {
List<Contact> contactList = new ArrayList<Contact>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMERGENCY_CONTACTS;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Contact contact = new Contact();
contact.setID(Integer.parseInt(cursor.getString(0)));
contact.setName(cursor.getString(1));
contact.setPhoneNumber(cursor.getString(2));
contact.setEmailId(cursor.getString(2));
// Adding contact to list
contactList.add(contact);
} while (cursor.moveToNext());
}
// return contact list
return contactList;
} */
// Updating single contact
public int updateEmergencyContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID, contact.getID());
values.put(KEY_NAME, contact.getName());
values.put(KEY_PH_NO, contact.getPhoneNumber());
values.put(KEY_EMAIL_ID, contact.getEmailId());
// updating row
return db.update(TABLE_EMERGENCY_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
}
public int updateUserInfo(UserInfo userInfo) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_USER_ID, userInfo.getID());
values.put(KEY_USER_NAME, userInfo.getName());
values.put(KEY_APP_PASSWORD, userInfo.getPassword());
// updating row
return db.update(TABLE_PERSONAL_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(userInfo.getID()) });
}
// Deleting single contact
/* public void deleteContact(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMERGENCY_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(contact.getID()) });
db.close();
} */
// Getting contacts Count
/* public int getContactsCount() {
String countQuery = "SELECT * FROM " + TABLE_EMERGENCY_CONTACTS;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
} */
}
这是错误日志:
4-13 12:20:08.727: D/table(754): table created 1
04-13 12:20:08.747: D/table(754): table created 2
04-13 12:20:08.757: D/AndroidRuntime(754): Shutting down VM
04-13 12:20:08.766: W/dalvikvm(754): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
04-13 12:20:08.787: E/AndroidRuntime(754): FATAL EXCEPTION: main
04-13 12:20:08.787: E/AndroidRuntime(754): java.lang.RuntimeException: Unable to start activity ComponentInfo{moin.WPG.databasecheck/moin.WPG.databasecheck.WPGDBActivity}: java.lang.IllegalStateException: getWritableDatabase called recursively
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread.access$600(ActivityThread.java:122)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.os.Handler.dispatchMessage(Handler.java:99)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.os.Looper.loop(Looper.java:137)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-13 12:20:08.787: E/AndroidRuntime(754): at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:20:08.787: E/AndroidRuntime(754): at java.lang.reflect.Method.invoke(Method.java:511)
04-13 12:20:08.787: E/AndroidRuntime(754): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-13 12:20:08.787: E/AndroidRuntime(754): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-13 12:20:08.787: E/AndroidRuntime(754): at dalvik.system.NativeStart.main(Native Method)
04-13 12:20:08.787: E/AndroidRuntime(754): Caused by: java.lang.IllegalStateException: getWritableDatabase called recursively
04-13 12:20:08.787: E/AndroidRuntime(754): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:140)
04-13 12:20:08.787: E/AndroidRuntime(754): at moin.WPG.databasecheck.DatabaseHandler.onCreate(DatabaseHandler.java:57)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:165)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:231)
04-13 12:20:08.787: E/AndroidRuntime(754): at moin.WPG.databasecheck.DatabaseHandler.getEmergencyContact(DatabaseHandler.java:122)
04-13 12:20:08.787: E/AndroidRuntime(754): at moin.WPG.databasecheck.WPGDBActivity.onCreate(WPGDBActivity.java:46)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.Activity.performCreate(Activity.java:4465)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-13 12:20:08.787: E/AndroidRuntime(754): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
04-13 12:20:08.787: E/AndroidRuntime(754): ... 11 more