0

我已经使用以下类成功地将数据插入到表中,但我不知道如何访问这些数据。另外,我想将数据表及其列内容放在数组列表中,例如

column1--->array1
column1--->array2
column1--->array3
column1--->array4
public class SQLiteAdapter {

 public static final String MYDATABASE_NAME = "MY_DATABASE";
 public static final String MYDATABASE_TABLE = "MY_TABLE";
 public static final int MYDATABASE_VERSION = 1;
 public static final String KEY_ID = "_id";
 public static final String KEY_CONTENT1 = "Content1";
 public static final String KEY_CONTENT2 = "Content2";
 public static final String KEY_CONTENT3 = "Content3";
 public static final String KEY_CONTENT4 = "Content4";



 //create table MY_DATABASE (ID integer primary key, Content text not null);
 private static final String SCRIPT_CREATE_DATABASE =
  "create table " + MYDATABASE_TABLE + " ("
  + KEY_ID + " integer primary key autoincrement, "
  + KEY_CONTENT1 + " text not null, "
  + KEY_CONTENT2 + " text not null, "
  + KEY_CONTENT3 + " text not null, "
  + KEY_CONTENT4 + " text not null);";

 private SQLiteHelper sqLiteHelper;
 private SQLiteDatabase sqLiteDatabase;

 private Context context;

 public SQLiteAdapter(Context c){

     this.context=c;
 }

 public SQLiteAdapter openToRead() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getReadableDatabase();
  return this; 
 }

 public SQLiteAdapter openToWrite() throws android.database.SQLException {
  sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
  sqLiteDatabase = sqLiteHelper.getWritableDatabase();
  return this; 
 }

 public void close(){
  sqLiteHelper.close();
 }

 public long insert(String content1, String content2, String content3, String content4){

  ContentValues contentValues = new ContentValues();
  contentValues.put(KEY_CONTENT1, content1);
  contentValues.put(KEY_CONTENT2, content2);
  contentValues.put(KEY_CONTENT3, content3);
  contentValues.put(KEY_CONTENT4, content4);


  return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
 }

 public int deleteAll(){
  return sqLiteDatabase.delete(MYDATABASE_TABLE, null, null);
 }

 public Cursor queueAll(){
  String[] columns = new String[]{KEY_ID, KEY_CONTENT1, KEY_CONTENT2, KEY_CONTENT3, KEY_CONTENT4};
  Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE, columns,
    null, null, null, null, null);

  return cursor;
 }

 public class SQLiteHelper extends SQLiteOpenHelper {

  public SQLiteHelper(Context context, String name,
    CursorFactory factory, int version) {
   super(context, name, factory, version);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
   // TODO Auto-generated method stub
   db.execSQL(SCRIPT_CREATE_DATABASE);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO Auto-generated method stub
  }
 } 
}
4

1 回答 1

3

要访问数据库,请尝试此操作。

首先,您必须拥有一个扩展 SQLiteOpenHelper 的类。创建一个扩展SQLiteOpenHelper的新类,并在主数据库打开类中调用它们。

在这里,您可以添加这两个块并在您的onCreate方法中调用它们

public Cursor getShow()
{
    SQLiteDatabase db = obj.getReadableDatabase();
    Cursor c = db.rawQuery("select * from emp", null);
    startManagingCursor(c);
    return c;
}

public Cursor getEvent(Cursor c)
{
    StringBuilder sb= new StringBuilder("Data Base:-");
    while(c.moveToNext())
    {
        long id = c.getLong(0);
        String un = c.getString(1);
        String pw = c.getString(2);
        String ut = c.getString(3);
        sb.append("\n"+"id:-"+id+
            "\n"+"Username:-"+un+
            "\n"+"Password:-"+pw+"\n"+
            "\n"+"UserType:-"+ut+"\n"
        );
    }
    tvTools.setText(sb);
    return c;
}

这会获取我之前创建的三列并将它们显示在TextView. 现在调用这些方法onCreate()

Cursor c = getShow();
getEvent(c);

它每次都对我有用。

于 2013-02-22T20:14:18.477 回答