检查SQLITE数据库是否为空将冻结应用程序



我发现以下线程检查数据库是否为空。但给出的解决方案似乎对我不起作用。有人能给我一个如何进行的提示吗?我是SQlite和Android工作室的新手。感谢

这就是我要做的。

public class MainActivity extends AppCompatActivity {
    public static boolean firstOrNot = false;
    static User user;
    MyDBHandler myDb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        myDb = new MyDBHandler(this);

我初始化数据库变量。

myProgressBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            if(myDb.isNotEmpty() != true) 

下面是出现问题的地方。当我调用这个方法时,它似乎完全冻结了我的程序。我希望它返回一个布尔值,看看数据库是否为空。这就是方法的外观

public boolean isNotEmpty(){
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    Boolean rowExists;
    if (mCursor.moveToFirst())
    {
        // DO SOMETHING WITH CURSOR
        rowExists = true;
    } else
    {
        // I AM EMPTY
        rowExists = false;
    }
    return rowExists;
}

这就是MyDBHandler类的其余部分的样子。

package com.example.marko.lektion2;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.content.Context;
import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

public class MyDBHandler extends  SQLiteOpenHelper{
    private static final String TAG = "DatabaseHelper";

    public static final String TABLE_NAME  ="REGISTRATION";
    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_WEIGHT = "WEIGHT";
    public static final String COLUMN_AGE = "AGE";
    private static final String[] COLUMNS = { COLUMN_ID, COLUMN_WEIGHT, COLUMN_AGE};

    public MyDBHandler(Context context ) {
        super(context, TABLE_NAME, null , 1);
      //  SQLiteDatabase db = this.getWritableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String createTable = "CREATE TABLE USER (" +  "id INTEGER PRIMARY KEY AUTOINCREMENT ," +
                "weight INTEGER ," + " age INTEGER)  ";
            db.execSQL(createTable);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int i, int i1) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }

    public User getUser(int id) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, // a. table
                COLUMNS, // b. column names
                " id = ?", // c. selections
                new String[] { String.valueOf(id) }, // d. selections args
                null, // e. group by
                null, // f. having
                null, // g. order by
                null); // h. limit
        if (cursor != null)
            cursor.moveToFirst();
        User user = new User();
        user.setId(Integer.parseInt(cursor.getString(0)));
        user.setWeight (Integer.parseInt(cursor.getString(1)));
        user.setAge(Integer.parseInt(cursor.getString(2)));
        return user;
    }

    public void addUser(User player) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_WEIGHT, player.getWeight());
        values.put(COLUMN_AGE, player.getAge());
        // insert
        db.insert(TABLE_NAME,null, values);
        db.close();
    }

MyDBHandler类中,这一行:

public static final String TABLE_NAME  ="REGISTRATION";

这个:

String createTable = "CREATE TABLE USER (" +  "id INTEGER PRIMARY KEY AUTOINCREMENT ," + "weight INTEGER ," + " age INTEGER)  ";
db.execSQL(createTable);

冲突。

你的桌子叫什么名字?

REGISTRATION还是USER
在方法isNotEmpty()中,您使用TABLE_NAME,即REGISTRATION,但您创建了一个名为USER的表

确定表的名称,更改代码,
从设备/模拟器卸载应用程序,删除数据库,然后重新运行以重新创建数据库
请重新填充表,然后重试您的代码。

这里有很多问题

1.(不要在UI线程上进行数据库查询

2.(您可以使用轻松减少此呼叫所需的时间

Cursor mCursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " LIMIT 1", null);

3.(如果它不是成员字段,就不要称它为mCursor,只需将其称为cursor 即可

4.(您没有关闭光标,您应该在末尾的finally {块中关闭它。

Cursor cursor = null;
try {
    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME + " LIMIT 1", null);
    ...
} finally {
    if(cursor != null) {
        cursor.close();
    }
}

我很确定这也有一个皮棉检查。

最新更新