关于android:将外部Sqlite与SimpleCursorAdapter一起使用时应用崩溃

App Crash while using External Sqlite with SimpleCursor Adapter

在学习了很多教程之后,我正在创建一个应用程序

该应用程序包含一个外部sqlite数据库。现在,当我尝试显示我的应用程序崩溃时,我正尝试在列表视图中显示第一列。存在,但是在我的数据库中我没有像column_id plz这样的列,这是我的下面的代码

我在SqliteManager中创建的语句

1
CREATE TABLE"Ayervedic" ("Item No" NUMERIC NOT NULL ,"Title" VARCHAR NOT NULL ,"Subcategory" VARCHAR NOT NULL ,"Details" VARCHAR NOT NULL ,"Images" VARCHAR NOT NULL , PRIMARY KEY ("Item No","Title","Subcategory","Details","Images"))

数据库类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class SqlLiteDbHelper extends SQLiteOpenHelper {

// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME ="Ayervedic.sqlite";
private static final String DB_PATH_SUFFIX ="/databases/";
static Context ctx;

public SqlLiteDbHelper(Context context) {

    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    ctx = context;
}
public void CopyDataBaseFromAsset() throws IOException {

    InputStream myInput = ctx.getAssets().open(DATABASE_NAME);

    // Path to the just created empty db
    String outFileName = getDatabasePath();

    // if the path doesn't exist first, create it
    File f = new File(ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX);
    if (!f.exists())
        f.mkdir();

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);


    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);

    }
    // Close the streams

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

private static String getDatabasePath() {

    return ctx.getApplicationInfo().dataDir + DB_PATH_SUFFIX + DATABASE_NAME;

}

public SQLiteDatabase openDataBase() throws SQLException {

    File dbFile = ctx.getDatabasePath(DATABASE_NAME);
    if (!dbFile.exists()) {
        try {
            CopyDataBaseFromAsset();

            System.out.println("Copying sucess from Assets folder");

        } catch (IOException e) {

            throw new RuntimeException("Error creating source database", e);

        }

    }
    return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);

}

@Override
public void onCreate(SQLiteDatabase db) {

}

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

}
public Cursor gettitles(SQLiteDatabase db)
{
    db = this.getReadableDatabase();

    Cursor cursor;

    cursor = db.query(true,"Ayervedic", new String[]{"Title"}, null, null, null, null, null, null);
    return cursor;
}

主要活动

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class MainActivity extends AppCompatActivity {

ListView listView;
String title;
SqlLiteDbHelper dbHelper;

SQLiteDatabase sqLiteDatabase;
Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   listView= (ListView) findViewById(R.id.listView);
    dbHelper = new SqlLiteDbHelper(this);
    try {
        dbHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    sqLiteDatabase=dbHelper.getReadableDatabase();
    cursor=dbHelper.gettitles(sqLiteDatabase);
    String[] from = new String[] {"Title" };
    int[] to = new int[] {R.id.textView };
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row_title,cursor,from,to);
    adapter.notifyDataSetChanged();
    listView.setAdapter(adapter);
}

Logcat:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Process: com.example.ky.tamil, PID: 6286
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ky.tamil/com.example.aeiltech.tamil.MainActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
        at android.app.ActivityThread.access$900(ActivityThread.java:179)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:146)
        at android.app.ActivityThread.main(ActivityThread.java:5641)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.IllegalArgumentException: column '_id' does not exist


尝试一下

1
Cursor c = db.rawQuery(" SELECT"+ Title +" AS _id from Ayervedic");

表示您选择标题,并使用AS在_id中为其创建别名,并且您正在从Ayervedic表中选择此ID。因此,现在您将能够从列名_id访问此查询的结果,并且为了访问结果,请使用:

1
2
3
4
5
c.moveToFirst();    
    while (c.moveToNext())
    {
     System.out.println(c.getString(c.getColumnIndex("_id"));
    }


您的SimpleCursor适配器需要使用_id行。

1
2
3
4
5
6
7
8
9
CREATE TABLE"Ayervedic" (
  "_id" INT AUTOINCREMENT,
  "Item No" NUMERIC NOT NULL,
  "Title" VARCHAR NOT NULL,
  "Subcategory" VARCHAR NOT NULL,
  "Details" VARCHAR NOT NULL ,
  "Images" VARCHAR NOT NULL ,
   PRIMARY KEY ("_id")
);


SimpleCursorAdapter是CursorAdapter的子类。文档指出:

The Cursor must include a column named"_id" or this class will not
work.

在您的情况下,您可以将主键Item No重命名为_id

编辑:即使没有将其映射到视图,您也需要在光标中选择此列。

1
cursor = db.query(true,"Ayervedic", new String[]{"Title","_id"}, null, null, null, null, null, null);