从SQLite数据库加载数据不起作用

时间:2018-06-03 12:17:01

标签: android database sqlite load android-sqlite

我有一个名为“TabliceBaza”的数据库

public class TabliceBaza extends SQLiteOpenHelper {

public static final String DATABASE_NAME="Tablica.db";
public static final String TABLE_NAME="Tablice_table";
public static final String COL1="_id";
public static final String COL2="Tablica";

public TabliceBaza(Context context) {
    super(context,DATABASE_NAME,null,1);
}

@Override
public void onCreate(SQLiteDatabase db) {

 db.execSQL("CREATE TABLE "+TABLE_NAME+"("+COL1+"INT PRIMARY KEY,"+COL2+"TEXT)");


}

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

}

public String loadHandler()
{
    String result = "";
    String query = "SELECT * FROM "+TABLE_NAME;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    while (cursor.moveToNext()) {
        int result_0 = cursor.getInt(0);
        String result_1 = cursor.getString(1);

        result += String.valueOf(result_0) + " " + result_1 +
                System.getProperty("line.separator");
    }
    cursor.close();
    db.close();
    return result;

}
public void addHandler(Tablice tablice) {
    ContentValues values = new ContentValues();
    values.put(COL1, tablice.getTablicaID());
    values.put(COL2,tablice.getTablicaIme());
    SQLiteDatabase db = this.getWritableDatabase();
    db.insert(TABLE_NAME, null, values);
    db.close();
}

在活动类中,我想显示数据库中的所有值并将其设置为TextView


以下是活动中ButtonClick的代码


   TabliceBaza tb=new TabliceBaza(this);
   TextView txt=(TextView)findViewById(R.id.textView3);

   Tablice t=new Tablice(1,"Misko00");
   tb.addHandler(t);
   txt.setText(tb.loadHandler());


正如你所看到的,当我点击按钮时,它应该在数据库中添加一个“Tablice”对象然后显示它,但它什么也没显示。

1 个答案:

答案 0 :(得分:0)

您遇到的主要问题是列名和列类型之间省略了空格。这将起作用,但会导致列名称与给定的列名称连接。因此,当您运行应用程序时,您将拥有一个名为 Tablice_table 的表,其中包含两列名称 _idINT TablicaTEXT

单击该按钮时,不会插入任何记录,因为 _id Tablica 列不存在。

如果您查看日志,那么您会看到类似的内容( 注意这不会导致应用程序崩溃 ): -

06-03 20:29:56.109 1143-1143/soanswers.soanswers E/SQLiteLog: (1) table Tablice_table has no column named _id
06-03 20:29:56.109 1143-1143/soanswers.soanswers E/SQLiteDatabase: Error inserting _id=1 Tablica=Test
    android.database.sqlite.SQLiteException: table Tablice_table has no column named _id (code 1): , while compiling: INSERT INTO Tablice_table(_id,Tablica) VALUES (?,?)
        at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
        at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
        at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
        at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
        at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
        at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
        at soanswers.soanswers.TabliceBaza.addHandler(TabliceBaza.java:56)
        at soanswers.soanswers.MainActivity$1.onClick(MainActivity.java:529)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16966)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

此问题的解决方法是修改代码以在创建表格时包含空格,例如: -

db.execSQL("CREATE TABLE " + TABLE_NAME + "(" + COL1 + " INT PRIMARY KEY," + COL2 + " TEXT)");

然后删除应用程序的数据或卸载应用程序。

删除应用程序的数据或卸载应用程序后重新运行,您提供的代码(在将空格添加到create table语句之后)可以正常工作。

但是,如果您再次单击该按钮,则会在日志中显示一条消息( 再次,这不会导致应用 崩溃): -

06-03 20:38:24.451 1256-1256/soanswers.soanswers E/SQLiteDatabase: Error inserting _id=1 Tablica=Test
    android.database.sqlite.SQLiteConstraintException: column _id is not unique (code 19)
        at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
        at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
        at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
        at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
        at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
        at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
        at soanswers.soanswers.TabliceBaza.addHandler(TabliceBaza.java:54)
        at soanswers.soanswers.MainActivity$1.onClick(MainActivity.java:529)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16966)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

这是因为 _id 是主键并且必须是unqiue因此尝试再次将 _id 添加为1的行将失败。