-3

连接到 Sqlite 数据库时会显示一些错误。而且还需要在每个设备中创建单独的 Sqlite 数据库。如何自动创建一个?

4

1 回答 1

0
 public Data getdata(int id) {
  Cursor cursor = db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
                   KEY_NAME, KEY_RANK }, KEY_ID + "=?",
                    new String[] { String.valueOf(id) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();

            Data data = new Data(Integer.parseInt(cursor.getString(0)),
                    cursor.getString(1), cursor.getString(2));
            // return name
            return data;
}

创建一个活动类并使用您的模型类调用读取数据参数

 txt = (TextView) findViewById(R.id.textView4);
            txt1 = (TextView) findViewById(R.id.textView5);
            txt2 = (TextView) findViewById(R.id.textView6);

            bt.setOnClickListener(new OnClickListener() {



                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    viewData();
                }

                private void viewData() {
                    // TODO Auto-generated method stub
                    try {
                        // ask the database manager for data
                        name = db.getdata(id);
                        txt.setText(name._name);
                        txt1.setText(name._rank);
    }
}

你的 onCreate(SQLitedatabase db)

public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                        KEY_NAME + " TEXT NOT NULL, " +
                        KEY_RANK + " TEXT NOT NULL "+ ")");

}

于 2012-10-02T06:56:04.423 回答