在android代码中,我正确地声明了所有列名。但是仍在运行时显示错误列缺失。在android代码中我正确地声明了所有列名。但是仍在运行它显示错误列缺失
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";
// 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_STAT = "stat";
private static final String KEY_PACK = "pack";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_NAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE, "
+ KEY_UID + " TEXT,"
+ KEY_STAT + "TEXT,"
+ KEY_PACK + " TEXT" + ")";
db.execSQL(CREATE_LOGIN_TABLE);
}
的adduser()
public void addUser(String name, String email, String uid, String stat, String pack) {
//public void addUser(String name, String email) {
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_STAT, stat); // Created At
values.put(KEY_PACK, pack);
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
db.close(); // Closing database connection
}
logcat的
03-05 07:14:42.579: E/SQLiteLog(1454): (1) table login has no column named stat
03-05 07:14:42.630: E/SQLiteDatabase(1454): Error inserting uid=7 pack=3 email=shank15@hotmail.com stat=1 name=Shashank
03-05 07:14:42.630: E/SQLiteDatabase(1454): android.database.sqlite.SQLiteException: table login has no column named stat (code 1): , while compiling: INSERT INTO login(uid,pack,email,stat,name) VALUES (?,?,?,?,?)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
03-05 07:14:42.630: E/SQLiteDatabase(1454): at com.example.androidhive.library.DatabaseHandler.addUser(DatabaseHandler.java:77)
答案 0 :(得分:3)
在列名称和类型之间添加缺少的空格。变化
+ KEY_STAT + "TEXT,"
到
+ KEY_STAT + " TEXT,"
修改onCreate()
后,卸载您的应用,以便重新创建数据库。有关详情,请参阅When is SQLiteOpenHelper onCreate() / onUpgrade() run?。