sqlite没有这样的表

时间:2012-11-23 17:38:06

标签: android database sqlite mobile

任何人都可以请帮助。我看到很多人遇到同样的问题并查看了所有的建议,但仍然无法使其发挥作用。我试图取消应用程序并再次安装,我试图更改版本号并重新开始。我已经调试了代码,它确实进入了onCreate函数,但是当我去做一个select查询时,它说用户表不存在。任何帮助将不胜感激。谢谢你们

DatabaseHandler类

public class DatabaseHandler extends SQLiteOpenHelper 
{   
    // Variables
    protected static final int DATABASE_VERSION = 1;            
    protected static final String DATABASE_NAME = "MyUser.db";

    // Constructor
    public DatabaseHandler(Context context) 
    { 
        super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

    // Creating Tables 
    @Override
    public void onCreate(SQLiteDatabase db) 
    {           
        // Create the Users table
        // NOTE: I have the column variables saved above
        String CREATE_USERS_TABLE = "CREATE TABLE IF NOT EXISTS Users("
  + KEY_PRIMARY_ID + " " + INTEGER + " " + PRIMARY_KEY + " " + AUTO_INCREMENT + " " + NOT_NULL + "," + USERS_KEY_EMAIL + " " + NVARCHAR+"(1000)" + " " + UNIQUE + " " + NOT_NULL + "," + USERS_KEY_PIN + " " + NVARCHAR+"(10)" + " " + NOT_NULL + ")"; 
  db.execSQL(CREATE_USERS_TABLE);
    }

    // Upgrading database 
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    { 
      db.execSQL("DROP TABLE IF EXISTS Users"); 
        onCreate(db); 
    }

UserDataSource类

public class UserDataSource 
{
    private SQLiteDatabase db;  
    private DatabaseHandler dbHandler;

    public UserDataSource(Context context) 
    {
        dbHandler = new DatabaseHandler(context);
    }

    public void OpenWriteable() throws SQLException 
    {
        db = dbHandler.getWritableDatabase();
    }

    public void Close() 
    {
        dbHandler.close();
    }

    // Validate the user login with the username and password provided
    public void ValidateLogin(String username, String pin) throws CustomException
    {   
         Cursor cursor = db.rawQuery(   "select * from Users where " + DatabaseHandler.USERS_KEY_EMAIL + " = '" + username + "'" + " and " + DatabaseHandler.USERS_KEY_PIN + " = '" + pin + "'" , null);

         ........
    }

然后在活动课上,我正在呼叫

UserDataSource uds = new UserDataSource (this);
uds.OpenWriteable();
uds.ValidateLogin("name", "pin");

任何帮助都会很棒,非常感谢 格雷厄姆

以下是错误报告中附加的日志

11-23 17:47:46.414: I/SqliteDatabaseCpp(26717): sqlite returned: error code = 1, msg = no such table: Users, db=/data/data/prometric.myitemwriter/databases/MyUser.db
11-23 17:47:57.085: D/AndroidRuntime(26717): Shutting down VM
11-23 17:47:57.085: W/dalvikvm(26717): threadid=1: thread exiting with uncaught exception (group=0x40bec1f8)
11-23 17:47:57.171: D/dalvikvm(26717): GC_CONCURRENT freed 575K, 8% free 8649K/9351K, paused 2ms+6ms
11-23 17:47:57.179: E/AndroidRuntime(26717): FATAL EXCEPTION: main
11-23 17:47:57.179: E/AndroidRuntime(26717): java.lang.IllegalStateException: Could not execute method of the activity
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.view.View$1.onClick(View.java:3091)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.view.View.performClick(View.java:3558)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.view.View$PerformClick.run(View.java:14152)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.os.Handler.handleCallback(Handler.java:605)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.os.Handler.dispatchMessage(Handler.java:92)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.os.Looper.loop(Looper.java:137)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.app.ActivityThread.main(ActivityThread.java:4514)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at java.lang.reflect.Method.invoke(Method.java:511)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at dalvik.system.NativeStart.main(Native Method)
11-23 17:47:57.179: E/AndroidRuntime(26717): Caused by: java.lang.reflect.InvocationTargetException
11-23 17:47:57.179: E/AndroidRuntime(26717):    at java.lang.reflect.Method.invokeNative(Native Method)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at java.lang.reflect.Method.invoke(Method.java:511)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.view.View$1.onClick(View.java:3086)
11-23 17:47:57.179: E/AndroidRuntime(26717):    ... 11 more
11-23 17:47:57.179: E/AndroidRuntime(26717): Caused by: android.database.sqlite.SQLiteException: no such table: Users: , while compiling: select * from Users where email = '' and pin = ''
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:127)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:94)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:53)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1685)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1659)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at projectname.database.UserDataSource.ValidateLogin(UserDataSource.java:73)
11-23 17:47:57.179: E/AndroidRuntime(26717):    at projectname.LoginActivity.btn_login_Click(LoginActivity.java:47)
11-23 17:47:57.179: E/AndroidRuntime(26717):    ... 14 more

5 个答案:

答案 0 :(得分:3)

您没有显示所有变量,但我猜测CREATE_USERS_TABLE语句出了问题。让我们组织并简化它:

"CREATE TABLE IF NOT EXISTS Users(" +
    KEY_PRIMARY_ID + " INTEGER PRIMARY KEY, " + 
    USERS_KEY_EMAIL + " NVARCHAR(1000) UNIQUE NOT NULL, " + 
    USERS_KEY_PIN + " NVARCHAR(10) NOT NULL)";

您需要通过将DATABASE_VERSION更改为2来告知数据库需要更新SQL架构。


此外,您应该在查询中使用selectionArgs参数来保护自己免受SQL注入攻击:

public void ValidateLogin(String username, String pin) throws CustomException
{   
     Cursor cursor = db.rawQuery(   "select * from Users where " + DatabaseHandler.USERS_KEY_EMAIL + " = ? and " + DatabaseHandler.USERS_KEY_PIN + " = ?", 
             new String[] {username, pin});

最后,您应该遵守Java命名约定并使用小写字母启动所有方法:openWritable()close(), validateLogin()`等。

答案 1 :(得分:3)

感谢您的帮助。这不起作用的原因是因为没有提交数据库插入。如果有人有兴趣,以下是我解决问题的方法。

// Create the Users table
db.beginTransaction();
try {
    Log.d(DatabaseHandler.class.getName(), "Creating table " + TABLE_USERS);
    String CREATE_USERS_TABLE = "CREATE TABLE IF NOT EXISTS " + TABLE_USERS + "("
            + KEY_PRIMARY_ID + " " + INTEGER + " " + PRIMARY_KEY + ","
            + USERS_KEY_EMAIL + " " + NVARCHAR+"(1000)" + " " + UNIQUE + " " + NOT_NULL + ","
            + USERS_KEY_PIN + " " + NVARCHAR+"(10)" + " " + NOT_NULL + ");"; 
    db.execSQL(CREATE_USERS_TABLE);
    db.setTransactionSuccessful();
}
catch (SQLException e) {
    Log.e(DatabaseHandler.class.getName(), "Error Creating table " + TABLE_USERS + " ... " + e.getMessage());
    // Do whatever you want here
}
finally {
    db.endTransaction();
}

答案 2 :(得分:0)

改变你的 protected static final int DATABASE_VERSION = 1;protected static final int DATABASE_VERSION = 2; 它将解决异常。

答案 3 :(得分:0)

UserDataSource类中,您尝试在关闭数据库后选择数据。那是在编写database_close()方法之后。因此,关闭数据库本身后,您将尝试选择数据。我认为这就是问题的原因。并考虑在database_version课程中更新UserDataSource。尝试这两件事。希望这会有所帮助。

答案 4 :(得分:0)

在这种方法中:

public void ValidateLogin(String username, String pin) throws CustomException
{   
     Cursor cursor = db.rawQuery("select * from Users where " +
     DatabaseHandler.USERS_KEY_EMAIL + " = '" + username + "'" + " and " +
     DatabaseHandler.USERS_KEY_PIN + " = '" + pin + "'" , null);
     ........
}

替换代码

 Cursor cursor = db.rawQuery("select * from Users where"
 +DatabaseHandler.USERS_KEY_EMAIL+"='"+username+"'"+"and"+ 
 DatabaseHandler.USERS_KEY_PIN +"='"+pin+"'", null);

我认为额外的空间很重要。请试一试。我希望它有效。 如果你有解决方案,请告诉我。