當我的應用程序正在創建數據庫時,它似乎沒有創建'cardname'列。我一直在查看我的代碼,但無法找到我做錯了什麼。任何人都可以幫助我嗎?Android Studio SQLite錯誤:「沒有這樣的列..」
將對DBAdapter類
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_USERNAME = "name";
static final String KEY_PASSWORD = "password";
static final String KEY_CARDNUM = "cardnum";
static final String KEY_CARDNAME = "cardname";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "CPDB";
static final String DATABASE_TABLE = "Users";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE =
"create table Users (_id integer primary key autoincrement, "
+ "name text not null,"
+"password text not null,"
+"cardnum text not null,"
+ "cardname text not null"
+ ");";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter (Context ctx){
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super (context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
try{
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version" + oldVersion + "to" + newVersion + "which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS Users");
onCreate (db);
}
}
的將對DBAdapter類
//--retrieves cards for the user--
public Cursor getCards(String username) throws SQLException{
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_CARDNAME}, KEY_USERNAME + "= '" + username + "'", null, null, null, null,null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
主要活動等級(當錯誤發生時)內的查詢..
if (view.getId() == R.id.bttn_show)
{
EditText username = (EditText) findViewById(R.id.tf_username);
String tesco = "tesco";
DBAdapter db= new DBAdapter(this);
db.open();
Cursor c = db.getCards(username.getText().toString());
ArrayList<String> usersCards = new ArrayList<>();
if(c.moveToFirst()) {
do {
String aCard = c.getString(c.getColumnIndex("cardname"));
usersCards.add(aCard);
} while (c.moveToNext());
}
if (usersCards.contains(tesco)) {
ImageButton tb1 = (ImageButton) findViewById(R.id.ib_1);
tb1.setBackgroundResource(R.drawable.tesco);
}
else {
Toast.makeText(this, "Please Add a Card", Toast.LENGTH_LONG).show();
}
}
logcat的錯誤日誌
--------- beginning of crash
02-07 13:13:08.100 2109-2109/com.cardpocket.cardpocket E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cardpocket.cardpocket, PID: 2109
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: android.database.sqlite.SQLiteException: no such column: cardname (code 1): , while compiling: SELECT DISTINCT _id, name, cardname FROM Users WHERE name= 'cliff'
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnecti on.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init> (SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver. java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
at com.cardpocket.cardpocket.DBAdapter.getCards(DBAdapter.java:148)
at com.cardpocket.cardpocket.mainactivity.onClick(mainactivity.java:78)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4002)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
預先感謝您。
請寄更多代碼?更新數據庫版本11 – 2015-02-07 13:06:53
你在'KEY_CARDNAME'中使用了正確的列名嗎? – ved 2015-02-07 13:33:26
你是什麼意思ved? – Ash30 2015-02-07 13:38:17