当我在 SQLite 数据库中插入第二个表时,我的应用程序崩溃了

问题描述 投票:0回答:1

所以我想在数据库中创建两个表:一个包含电子邮件和密码,另一个包含用户信息。但是,当我调用方法“addUser_info”时,我的应用程序崩溃了。这是我的代码:

public class OpenHelper extends SQLiteOpenHelper {
    // Database and table data
    private static final String DATABASE_NAME = "coffee.db";
    private static final int DATABASE_VERSION = 1;
    private static final String TABLE_NAME1 = "Login";
    private static final String TABLE_NAME2 = "Inf";
    private static final String TESTER = " tester";
    // Column names
    private static final String COLUMN_ID = "id";
    private static final String COLUMN_ID_INFO = "user_id";
    private static final String COLUMN_ID_TESTER = "tester_id";
    private static final String COLUMN_TESTER_NAME = "user_id";
    private static final String COLUMN_TESTER_EMAIL = "user_id";
    private static final String COLUMN_EMAIL_INFO = "user_email";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_PASSWORD = "password";
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_AGE = "bdate";
    private static final String COLUMN_HOBBIES = "hobbies";
    private static final String COLUMN_ABOUT = "about";

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

    @Override
    public void onCreate(SQLiteDatabase db) {
        String query1 = "CREATE TABLE " + TABLE_NAME1 + " (" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_EMAIL + " TEXT, " +
            COLUMN_PASSWORD + " TEXT);";
        db.execSQL(query1);
        String query2 = "CREATE TABLE " + TABLE_NAME2 + " (" +
            COLUMN_ID_INFO + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            COLUMN_EMAIL_INFO + "TEXT, " +
            COLUMN_NAME + " TEXT, " +
            COLUMN_AGE + " INTEGER, " +
            COLUMN_HOBBIES + " TEXT, " +
            COLUMN_ABOUT + " TEXT);";
        db.execSQL(query2);
    }

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME1);
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
        onCreate(db);
    }

    public void addUser(String email, String password) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_EMAIL, email);
        values.put(COLUMN_PASSWORD, password);
        db.insert(TABLE_NAME1, null, values);
        db.close();
    }

    public void addUser_info(String email, String name, int age, String about, String hobby) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_EMAIL_INFO, email);
        values.put(COLUMN_NAME, name);
        values.put(COLUMN_AGE, age);
        values.put(COLUMN_ABOUT, about);
        values.put(COLUMN_HOBBIES, hobby);
        db.insert(TABLE_NAME2, null, values);
        db.close();
    }

    public Boolean logIn(String email, String password) {
        int count = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME1 + " where password =? and email=?", new String[] {
            password,
            email
        });
        if (cursor != null) {
            try {
                count = cursor.getCount();
            } finally {
                cursor.close();
            }
        }
        if (count > 0)
            return true;
        else
            return false;
    }

    public Boolean checkReg(String email) {
        int count = 0;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME1 + " where  email=?", new String[] {
            email
        });
        if (cursor != null) {
            try {
                count = cursor.getCount();
            } finally {
                cursor.close();
            }
        }
        if (count > 0)
            return true;
        else
            return false;
    }

    public HashMap < String, String > getUserDetails() {
        HashMap < String, String > user = new HashMap < String, String > ();
        String selectQuery = "SELECT  * FROM " + TABLE_NAME1;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("login", cursor.getString(1));
            user.put("password", cursor.getString(2));
        }
        cursor.close();
        return user;
    }

    public HashMap < String, String > getUsersDetails(String email) {
        HashMap < String, String > user = new HashMap < String, String > ();
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from " + TABLE_NAME2 + " where  email=?", new String[] {
            email
        });
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            user.put("name", cursor.getString(1));
            user.put("email", cursor.getString(2));
        }
        cursor.close();
        return user;
    }
}
java android sqlite
1个回答
0
投票

好的,检查一下是否有语法错误?另外尝试将您的 DATABASE_VERSION 升级到 2 或除 1 之外的任何随机数,祝一切顺利..

© www.soinside.com 2019 - 2024. All rights reserved.