使用带有SimpleCursor适配器的外部Sqlite时应用程序崩溃

时间:2015-10-15 10:59:37

标签: android sqlite simplecursoradapter crash

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

应用程序包含一个外部sqlite数据库..现在我正在尝试显示列表视图中的第一列当我试图显示我的应用程序崩溃时..当我检查logcat它只是说列'_id '不存在,但在我的数据库中,我没有像column_id那样的列plz帮助,这里是我的代码

我在SqliteManager中的创建语句

Public Function methodName(input As methodName) As methodNameResponse Implements IService.methodName
…
            Dim ListResponse As New methodNameResponse()
            ListResponse.return = ListeDataType.ToArray
            Return ListResponse 
End Function

数据库类

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"))

主要活动

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;
}

logcat的:

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);
}

3 个答案:

答案 0 :(得分:1)

SimpleCursorAdapter是CursorAdapter的子类。文档states

  

Cursor必须包含名为" _id"的列。或者这个班不会   工作

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

编辑:您还需要在光标中选择此列,即使它未映射到视图。

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

答案 1 :(得分:1)

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

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")
);

答案 2 :(得分:1)

试试这个

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

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

c.moveToFirst();    
    while (c.moveToNext())
    {
     System.out.println(c.getString(c.getColumnIndex("_id")); 
    }