简体   繁体   中英

SQLiteDatabse giving unexpected errors

All files under my SQLite package are giving errors. As these are predefined classes and don't require or allow editing, I'm having a hard time making my project.

The codes are too long to be posted here. For instance, these imports in SQLiteDatabase.java are appearing in red:

import android.database.sqlite.SQLiteDebug.DbStats;
import dalvik.system.CloseGuard;

and my logcat shows the following errors:

02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: FATAL EXCEPTION: main
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime: android.database.sqlite.SQLiteException: no such table: user (code 1): , while compiling: select * from user
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at com.example.abcd.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:40)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at com.example.abcd.helloworld.SignUp$1.onClick(SignUp.java:52)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4240)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:17721)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:730)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 12:47:55.781 2666-2666/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

All coding done by me seems to be correct.

The code for table creation is:

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "user.db";
    private static final String TABLE_NAME = "user";
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;
    private static final String TABLE_CREATE = "create table user (id integer primary key autoincrement," +
            "name text, email text, uname text, pass text);";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db = db;
    }

    public void insertUser(User u) {
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        String query = "select * from user";
        Cursor cursor = db.rawQuery(query, null);
        int count = cursor.getCount();
        values.put(COLUMN_ID, count);
        values.put(COLUMN_NAME, u.getName());
        values.put(COLUMN_EMAIL, u.getEmail());
        values.put(COLUMN_UNAME, u.getUname());
        values.put(COLUMN_PASS, u.getPass());

        db.insert(TABLE_NAME, null, values);

    }

    public String searchPass(String uname) {
        db = this.getReadableDatabase();
        String query = "select uname, pass from" + TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a, b;
        b = "not found";
        if (cursor.moveToFirst()) {
            do {
                a = cursor.getString(0);

                if (a.equals(uname)) {
                    b = cursor.getString(1);
                    break;
                }
            }
            while (cursor.moveToNext());
        }
        db.close();
        return b;

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String query = "DROP TABLE IF EXISTS" + TABLE_NAME;
        db.execSQL(query);
        this.OnCreate(db);
    }

    private void OnCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
        this.db = db;
    }

}

You are using onCreate method twice in your code. Remove duplication.

Try below code:

public class DBHandler extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "user.db";

    public DBHandler(Context context) {
         super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query = "create table user(" +
                "id integer primary key autoincrement, " +
                "name text, "+
                "email text, "+
                "uname text, "+
                "pass text"+
                ");";

        db.execSQL(query);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        String query = "drop table if exists user";
        db.execSQL(query);

        onCreate(db);
    }

    /*

        Remaining code

        Please do not add onCreate and onUpgrade method anymore.

    */

}

Your code seems to be ok, try to debug the SQLite code. Execute the statement outside the android code. Open your terminal and execute the sql statement, and after that execute .tables, this command will show you the existed tables in your db.

More info, you can find here, http://www.tutorialspoint.com/sqlite/sqlite_create_table.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM