SQL Android的错误:没有这样的表 [英] SQL Android Error: no such table
问题描述
所以,我将数据输入到我的EditText场,它位于我AddContacts类每一次,我对话说,我已经成功地添加了信息,但在日志猫,它说没有这样的表:contactsTable。我认为错误是在我的数据库类的onCreate和OnUpdate中的方法,但我不知道该怎么做。
So every time I enter data into my EditText field, which is located in my AddContacts class, my Dialog says that I have successfully added the information, but in the log cat it says no such table: contactsTable. I think the error is within the onCreate and onUpdate methods in my database class, but I am not sure what to do
code:
package com.example.myfirstapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseTable {
private static final String TAG = "ContactsDatabase";
//The columns we'll include in the contacts table
public static final String COL_ID = "ID";
public static final String COL_NAME = "NAME";
public static final String COL_EMAIL = "EMAIL";
private static final String DATABASE_NAME = "CONTACT";
private static final String DATABASE_TABLE = "contactsTable";
private static final int DATABASE_VERSION = 1;
private Context ourContext;
private DbHelper DBHelper;
private SQLiteDatabase db;
private static final String DATABASE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
COL_NAME + " TEXT NOT NULL, " +
COL_EMAIL + " TEXT NOT NULL);";
private static class DbHelper extends SQLiteOpenHelper {
DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
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) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DatabaseTable(Context context) {
ourContext = context;
}
public DatabaseTable open() throws SQLException {
DBHelper = new DbHelper(ourContext);
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
public long insertContact(String name, String email) {
ContentValues initialValues = new ContentValues();
initialValues.put(COL_NAME, name);
initialValues.put(COL_EMAIL, email);
return db.insert(DATABASE_TABLE, null, initialValues);
}
public void deleteRecord(long rowId)
{
db.delete(DATABASE_TABLE, COL_ID + "=" + rowId, null);
}
public Cursor getAllRecords() {
return db.query(DATABASE_TABLE, new String[] {COL_ID, COL_NAME, COL_EMAIL}, null,
null, null, null, null);
}
public Cursor getRecord(long rowId) throws SQLException {
Cursor cursor = db.query(true, DATABASE_TABLE, new String[] {COL_ID, COL_NAME,
COL_EMAIL}, COL_ID + "=" + rowId, null, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
return cursor;
}
public boolean updateRecord(long rowId, String name, String email) {
ContentValues content = new ContentValues();
content.put(COL_NAME, name);
content.put(COL_EMAIL, email);
return db.update(DATABASE_TABLE, content, COL_ID + "=" + rowId, null) > 0;
}
}
推荐答案
你改变 DATABASE_TABLE
运行你的应用程序至少一次后?如果是这样,你需要更新你的SQLite模式,要做到这一点最简单的方法是让 DATABASE_VERSION = 2;
。另外Android是非常讲究的主键,你应该改变 COL_ID
到_ ID
。
Did you change DATABASE_TABLE
after running your app at least once? If so you need to update your SQLite schema, the easiest way to do this is make DATABASE_VERSION = 2;
. Also Android is very particular about the primary key, you should change COL_ID
to "_id"
.
在未来,你必须增加 DATABASE_VERSION
每次你改变你的 CREATE_TABLE
语句。否则的SQLite不会有当前的信息。
In the future, you must increment DATABASE_VERSION
every time you change your CREATE_TABLE
statement. Otherwise SQLite will not have the current information.
这篇关于SQL Android的错误:没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!