Android Sqlite数据库连接

时间:2012-10-02 06:32:45

标签: android sqlite

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

1 个答案:

答案 0 :(得分: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;
}

使用ur Model类

创建一个Activity类并调用read数据参数
 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 "+ ")");

}