实现SQLite数据库

时间:2017-11-27 09:43:28

标签: android android-sqlite

我一直在关注一个Android教程来实现SQLite数据库来注册用户。但是,当我的应用程序尝试验证用户名时(如果已经使用了用户名),它能够创建数据库它会抛出错误

FATAL EXCEPTION: main
                  Process: com.example.pc.codedelta, PID: 5617
                  android.database.sqlite.SQLiteException: no such column: USERNAME (code 1): , while compiling: SELECT * FROM people WHERE  USERNAME=?
                      at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                      at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                      at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                      at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                      at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                      at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                      at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                      at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1318)
                      at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1165)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1036)
                      at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1204)
                      at com.example.pc.codedelta.RegisterActivity.Checkingusername(RegisterActivity.java:117)
                      at com.example.pc.codedelta.RegisterActivity$1.onClick(RegisterActivity.java:56)
                      at android.view.View.performClick(View.java:6256)
                      at android.view.View$PerformClick.run(View.java:24697)
                      at android.os.Handler.handleCallback(Handler.java:789)
                      at android.os.Handler.dispatchMessage(Handler.java:98)
                      at android.os.Looper.loop(Looper.java:164)
                      at android.app.ActivityThread.main(ActivityThread.java:6541)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Application terminated.

这是我的程序崩溃的地方: (从光标开始的行崩溃)

public void Checkingusername()
{

        sqLiteDatabaseObj = sqLiteHelper.getWritableDatabase();
        cursor = sqLiteDatabaseObj.query(sqLiteHelper.TABLE_NAME,null," "+
                sqLiteHelper.COLUMN_USERNAME +"=?", new String[]{username},null,null,null,null);


        while(cursor.moveToNext()){
            if(cursor.isFirst()){
                cursor.moveToFirst();
                Result = "Found";

                cursor.close();
            }
        }
        CheckFinal();
    }

我的SQLiteHelper.java文件

public class SQLiteHelper extends SQLiteOpenHelper
{
    static String DATABASE_NAME ="tutorapp.db";
    public static final String TABLE_NAME = "people";
    public static final String TABLE_COL_ID="id";
    public static final String COLUMN_NAME ="name";

    public static final String COLUMN_PHONE ="PHONE";
    public static final String COLUMN_D0B = "DOB";
    public static final String COLUMN_USERNAME = "USERNAME";
    public static final String COLUMN_PASSWORD = "PASSWORD";

    private static final int DATABASE_VERSION = 1;

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

    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase)
    {
        String SQL_CREATE_TABLE ="CREATE TABLE " +
                TABLE_NAME + "(" + TABLE_COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_NAME + " VARCHAR, " +

                COLUMN_D0B + " VARCHAR, " +
                COLUMN_PHONE + " VARCHAR, " +
                COLUMN_USERNAME + " VARCHAR, "+
                COLUMN_PASSWORD + " VARCHAR)";

        sqLiteDatabase.execSQL(SQL_CREATE_TABLE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int oldVersion, int newVersion) {
        sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(sqLiteDatabase);
    }
}

任何帮助都将受到高度赞赏! 感谢

0 个答案:

没有答案