在创建表时遇到问题,每当我尝试查看联系人时,应用程序就会崩溃。基本上想添加联系人,查看联系人,更新和删除,正在添加联系人,但未创建表。 请帮忙!
我试图执行许多create语句,但似乎没有一个起作用。
这是我的代码:
public class contactDBHelper extends SQLiteOpenHelper {
public static final String Database_Name = "contact_db" ;
public static final int Database_Version = 1 ;
public static final String Table_Name = "contact_info";
public static final String Contact_ID = "contact_id";
public static final String Name = "name";
public static final String Email = "email";
public contactDBHelper (Context context) {
super(context,Database_Name,null,Database_Version);
Log.d("Database Operations","Database created ...");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(" CREATE TABLE " + Table_Name + " (" + Contact_ID + " TEXT PRIMARY KEY, " + Name + " TEXT NOT NULL, " + Email + " TEXT NOT NULL);");
Log.d("Database Operations","Table created ...");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + Table_Name);
onCreate(db);
}
public void addContact (int id, String name, String email, SQLiteDatabase database) {
ContentValues values = new ContentValues();
values.put(Contact_ID,id);
values.put(Name,name);
values.put(Email,email);
database.insert(Table_Name,null,values);
Log.d("Database Operations","One Row inserted...");
}
public Cursor fetch (SQLiteDatabase database){
String[] coloumns = new String[] {Contact_ID,Name,Email};
Cursor cursor = database.query(Table_Name,coloumns,null,null,null,null,null);
if (cursor != null) {
cursor.moveToFirst();
}
return cursor ;
}
}
错误
android.database.sqlite.SQLiteException: no such column: contact_id
(code 1 SQLITE_ERROR): , while compiling: SELECT contact_id, name,
email FROM contact_info
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native
Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:46)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1290)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1161)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1329)
at com.example.myappcontact.contactDBHelper.fetch(contactDBHelper.java:60)
at com.example.myappcontact.ReadContacts.readContacts(ReadContacts.java:41)
at com.example.myappcontact.ReadContacts.onCreateView(ReadContacts.java:32)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7116)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)