0

我创建了下表

public class LoginDBAdapter {
static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 2;
public static final String COL_ROWID= "rowid";
public static final String COL_USERNAME= "username";
public static final String COL_EMAIL= "email";
public static final String COL_PASSWORD= "password";
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
                             "( " +"rowid"+" integer primary key autoincrement,"+ "   username text,"+" password text,"+" email text); ";
// Variable to hold the database instance
public  SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DBHelper dbHelper;
public  LoginDBAdapter(Context _context) {
    context = _context;
    dbHelper = new DBHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public  LoginDBAdapter open() throws SQLException {
    db = dbHelper.getWritableDatabase();
    return this;
}
public void close() {
    db.close();
}
    public  SQLiteDatabase getDatabaseInstance(){
    return db;
}

public void insertEntry(String userName,String password,String email){
    ContentValues newValues = new ContentValues();
    // Assign values for each row.
    newValues.put("USERNAME", userName);
    newValues.put("PASSWORD",password);
    newValues.put("EMAIL", email);
    db.insert("LOGIN", null, newValues);
    ///Toast.makeText(context, "Reminder Is Successfully Saved",  Toast.LENGTH_LONG).show();
}
public int deleteEntry(String Email){
    //String id=String.valueOf(ID);
    String where="USERNAME=?";
    int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{Email}) ;
    // Toast.makeText(context, "Number fo Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
    return numberOFEntriesDeleted;
}   
  public String getSinlgeEntry(String Email){
Cursor cursor=db.query("LOGIN", null, " USERNAME=?", new String[]{Email}, null,    null, null);
     if(cursor.getCount()<1) {
        // UserName Not Exist
        cursor.close();
        return "NOT EXIST";
    }
    cursor.moveToFirst();
    String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
    cursor.close();
    return password;                
}
public void  updateEntry(String userName,String password,String email){
    // Define the updated row content.
    ContentValues updatedValues = new ContentValues();
    // Assign values for each row.
    updatedValues.put("USERNAME", userName);

    updatedValues.put("PASSWORD",password);

    updatedValues.put("EMAIL", email);

    String where="USERNAME = ?";
   db.update("LOGIN",updatedValues, where, new String[]{userName});               
}
}

我收到以下错误

06-11 13:48:00.512: E/SQLiteLog(938): (1) table LOGIN has no column named EMAIL
06-11 13:48:00.607: E/SQLiteDatabase(938): Error inserting EMAIL=aaa USERNAME=swe PASSWORD=111
06-11 13:48:00.607: E/SQLiteDatabase(938): android.database.sqlite.SQLiteException:  table LOGIN has no column named EMAIL (code 1): , while compiling: INSERT INTO   LOGIN(EMAIL,USERNAME,PASSWORD) VALUES (?,?,?)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at com.androidhive.loginandregister.LoginDBAdapter.insertEntry(LoginDBAdapter.java:55)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at com.androidhive.loginandregister.RegisterActivity$2.onClick(RegisterActivity.java:59)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.view.View.performClick(View.java:4204)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.view.View$PerformClick.run(View.java:17355)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.os.Handler.handleCallback(Handler.java:725)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.os.Looper.loop(Looper.java:137)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at java.lang.reflect.Method.invokeNative(Native Method)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at java.lang.reflect.Method.invoke(Method.java:511)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-11 13:48:00.607: E/SQLiteDatabase(938):  at dalvik.system.NativeStart.main(Native Method)
06-11 13:48:12.947: I/Choreographer(938): Skipped 79 frames!  The application may be doing too much work on its main thread.
06-11 13:48:13.287: D/dalvikvm(938): GC_CONCURRENT freed 155K, 9% free 3064K/3364K, paused 95ms+183ms, total 746ms
06-11 13:48:15.527: I/Choreographer(938): Skipped 238 frames!  The application may be doing too much work on its main thread.
06-11 13:48:16.347: I/Choreographer(938): Skipped 43 frames!  The application may be doing too much work on its main thread.
06-11 13:48:17.917: I/Choreographer(938): Skipped 89 frames!  The application may be doing too much work on its main thread.
06-11 13:48:21.047: I/Choreographer(938): Skipped 40 frames!  The application may be doing too much work on its main thread.
06-11 13:49:47.471: I/Choreographer(938): Skipped 33 frames!  The application may be doing too much work on its main thread.
06-11 13:50:06.797: I/Choreographer(938): Skipped 40 frames!  The application may be doing too much work on its main thread.

感谢帮助

4

4 回答 4

0

您的 create 语句和声明主键的方式似乎有错误。使用此创建语句:

static final String DATABASE_CREATE = "CREATE TABLE Login (rowid INTEGER NOT NULL, username TEXT, password TEXT, email TEXT, PRIMARY KEY (rowid));";
于 2013-06-11T14:42:40.170 回答
0

表仅在第一次创建并且 SQLite 文件已经存在,因此删除应用程序然后再次运行,直到创建具有新字段的新表。

于 2013-06-11T14:54:16.737 回答
0

这是由于列名区分大小写。您这样创建了数据库表

static final String DATABASE_CREATE = "create table "+"LOGIN"+
                         "( " +"rowid"+" integer primary key autoincrement,"+ 
                         "username text,"+" password text,"+" email text); ";

因此,您的列名是“rowid”、“用户名”、“密码”和“电子邮件”

更改以下内容

newValues.put("USERNAME", userName);
newValues.put("PASSWORD",password);
newValues.put("EMAIL", email);

newValues.put("username", userName);
newValues.put("password",password);
newValues.put("email", email);

此外,将“rowid”更改为“_id”,因为对于 Android SQLite,主键应始终称为“_id”

于 2013-06-12T06:17:21.020 回答
0

您应该尝试使用小写的字段名称作为jcwx所说的,例如:

newValues.put("email", email);

或者

newValues.put(COL_EMAIL, email);

因为您创建此字段(COL_EMAIL,..etc)而在您的代码中没有任何用途,并且您在名为“email”的数据库中的 EMAIL 字段您尝试将值添加到“EMAIL”,并且错误消息会通知您。

于 2013-09-18T08:16:40.063 回答