插入行后清空SQLite数据库

时间:2015-03-07 15:37:41

标签: android database sqlite insert row

我希望使用以下方法将数据存储到Android上的SQLite数据库中:

public void addMedicine(Medicine medicine)
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(COLUMN_NAME, medicine.get_name());
    values.put(COLUMN_FORMAT, medicine.get_format());
    values.put(COLUMN_AMOUNT, medicine.get_amount());
    values.put(COLUMN_EXP_DATE, medicine.get_exp_date());
    values.put(COLUMN_TIME, medicine.get_time());

    db.insert(TABLE_MEDICINES, null, values);

    db.close();
}

我传递给Medicine方法的addMedicine对象是我创建的类的一个实例,它可以工作。

当我想从TABLE_MEDICINES中选择数据时,我使用此功能:

public int getMedicines()
{
    String med = "";
    SQLiteDatabase db = getWritableDatabase();
    String SELECT_MEDICINES = "SELECT * FROM " + TABLE_MEDICINES;

    // Setting cursor to the first row.

    Cursor cursor = db.rawQuery(SELECT_MEDICINES, null);
    cursor.moveToFirst();

    int tmp_count = 0;

    while(!cursor.isAfterLast())
    {
        if(cursor.getString(cursor.getColumnIndex("name")) != null)
        {
            med += cursor.getString(cursor.getColumnIndex("name"));
            med += "\n";
            tmp_count++;
        }
    }

    db.close();

    return tmp_count;

}

我只计算表格中有多少行,并设置TextView的{​​{1}}值的文本。当我运行应用程序时,tmp_count始终为0,我认为数据库为空,因此没有插入行。 任何人都有任何想法?提前谢谢大家!

这是我的TextView课程,因此您可以了解我如何创建数据库:

DatabaseHandler

这是我调用public class DatabaseHandler extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 1; private static final String DATABASE_NAME = "DailyPill"; private static final String TABLE_MEDICINES = "Medicines"; private static final String COLUMN_ID = "_id"; private static final String COLUMN_NAME = "name"; private static final String COLUMN_FORMAT = "format"; private static final String COLUMN_AMOUNT = "amount"; private static final String COLUMN_EXP_DATE = "exp_date"; private static final String COLUMN_TIME = "time"; public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } public DatabaseHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler) { super(context, DATABASE_NAME, factory, DATABASE_VERSION, errorHandler); } @Override public void onCreate(SQLiteDatabase db) { String CREATE_QUERY = "CREATE TABLE " + TABLE_MEDICINES + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT" + ", " + COLUMN_NAME + " VARCHAR(50)" + ", " + COLUMN_FORMAT + " VARCHAR(20)" + ", " + COLUMN_AMOUNT + " INTEGER" + ", " + COLUMN_EXP_DATE + " DATE" + ", " + COLUMN_TIME + " HOUR" + ");"; db.execSQL(CREATE_QUERY); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS" + TABLE_MEDICINES); onCreate(db); } // The following functions adds a row to the database. public long addMedicine(Medicine medicine) { SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(COLUMN_NAME, medicine.get_name()); values.put(COLUMN_FORMAT, medicine.get_format()); values.put(COLUMN_AMOUNT, medicine.get_amount()); values.put(COLUMN_EXP_DATE, medicine.get_exp_date()); values.put(COLUMN_TIME, medicine.get_time()); long tmp = db.insert(TABLE_MEDICINES, null, values); db.close(); return tmp; } // The following function gets all rows from the database. public int getMedicines() { String[] Medicines = {}; String med = ""; SQLiteDatabase db = getWritableDatabase(); String SELECT_MEDICINES = "SELECT " + COLUMN_NAME +" FROM " + TABLE_MEDICINES; // Setting cursor to the first row. Cursor cursor = db.rawQuery(SELECT_MEDICINES, null); cursor.moveToFirst(); /*for(int i = 0; !cursor.isAfterLast(); i++) { if(cursor.getString(cursor.getColumnIndex(COLUMN_NAME)) != null) { Medicines[i] = cursor.getString(cursor.getColumnIndex(COLUMN_NAME)); } }*/ int tmp_count = 0; while(!cursor.isAfterLast()) { if(cursor.getString(cursor.getColumnIndex("name")) != null) { med += cursor.getString(cursor.getColumnIndex("name")); med += "\n"; tmp_count++; } } db.close(); return tmp_count; } public int tmp_getMedicines() { SQLiteDatabase db = this.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from " + TABLE_MEDICINES, null); int tmp_count = cursor.getCount(); cursor.close(); return tmp_count; } } 方法的函数:

addMedicine()

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = null; switch(mPage) { case 1: view = inflater.inflate(R.layout.layout_sqltest, container, false); Medicine med = new Medicine("medicine1", "pill", 1, "2018-02-01", "12:30:00"); dbHandler.addMedicine(med); temp_out(view, dbHandler); return view; case 2: view = inflater.inflate(R.layout.layout_settings, container, false); return view; case 3: view = inflater.inflate(R.layout.layout_info, container, false); return view; } return view; } 方法如下:

temp_out

2 个答案:

答案 0 :(得分:0)

您的getMedicines()函数从未调用cursor.moveToNext(),因此陷入无限循环。

答案 1 :(得分:0)

按如下方式更改您的getMedicines()方法 -

public int getMedicines() {
        String med = "";
        SQLiteDatabase db = getWritableDatabase();
        String SELECT_MEDICINES = "SELECT * FROM " + TABLE_MEDICINES;

        // Setting cursor to the first row.

        Cursor cursor = db.rawQuery(SELECT_MEDICINES, null);
        int tmp_count = 0;
        while (cursor.moveToNext()) {
            if (cursor.getString(cursor.getColumnIndex("name")) != null) {
                med += cursor.getString(cursor.getColumnIndex("name"));
                med += "\n";
                tmp_count++;
            }
        }

        db.close();
        return tmp_count;

    }

修改 如果你正在使用android studio并且已经在环境变量中设置了adb,那么你可以使用终端中的以下命令来查看模拟器和root设备上的数据库 -

>adb shell
# sqlite3 /data/data/yourpackagename/databses/yourdatabasename

然后你可以使用简单的sqlite查询来检查细节