本文介绍了SQLite的访问表的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经成功地使用下面的类表中插入的数据,但我不知道如何访问这些数据。另外,我想将数据表和数组列表的列内容,如
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
}
}
}
解决方案
To have access to a database, try this.
First you must be having a class that extends your SQLiteOpenHelper. Create a new class which extends SQLiteOpenHelper and call them in main database opening class.
Here you go add these two chunks and call them in you onCreate
method
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;
}
This fetches three columns that i have created earlier and display them on TextView
. Now call these method in onCreate()
.
Cursor c = getShow();
getEvent(c);
It works for me every time.
这篇关于SQLite的访问表的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!