我在Android应用程序中创建了这个类。但是,当我要从创建的数据库中检索数据时,它显示数据库不可用。请帮我这样做。感谢
public class DatabaseHandler extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "dbumbers";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "numbers";
public DatabaseHandler(Context context, String name, CursorFactory factory,
int version) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//Create database query
db.execSQL("create table " + TABLE_NAME + " (district TEXT, category TEXT, description TEXT, number TEXT); ");
//Insert query
db.execSQL("insert into " + TABLE_NAME + " values(Ampara,hospital,Ampara General Hospital,0112456852);");
db.execSQL("insert into " + TABLE_NAME + " values(Anuradhapura,bar,Anuradhapura Bar,011245852);");
db.execSQL("insert into " + TABLE_NAME + " values(Colombo,retail,Colombo Supermarket,0112546852);");
db.execSQL("insert into " + TABLE_NAME + " values(Gampaha,flight,Gampaha Airport,0112455552);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//add more insert query if you need to add more datas after, but you have first to upgrade your DATABASE_VERSION to a higher number
}
}
这是检索部分
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data_check);
SQLiteDatabase db = openOrCreateDatabase("dbumbers", 1, null);
Cursor c = db.rawQuery("SELECT * FROM numbers", null);
//c.moveToFirst();
while (c.moveToNext()) {
String district = c.getString(0);
String category = c.getString(1);
String description = c.getString(2);
String number = c.getString(2);
String row = district + "-" + category + "-" + description + "-" + number;
Toast.makeText(this, row, Toast.LENGTH_LONG).show();
}
db.close();
}
答案 0 :(得分:1)
您应该使用DatabaseHandler
打开数据库,而不是使用openOrCreateDatabase
。像这样更改构造函数(因为您只需要其中一个参数):
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
然后得到一个像这样的数据库:
DatabaseHandler dbHandler = new DatabaseHandler(this /*context*/);
SQLiteDatabase db = dbHandler.getReadableDatabase();
Cursor c = db.rawQuery(...);
答案 1 :(得分:0)
尝试将数据库名称更改为:
private static final String DATABASE_NAME = "dbumbers.db";
答案 2 :(得分:0)
你可以做这样的事情来使用SQLite
1)创建一个将扩展SQLieOpenHelper
的databaseHandler类public class EventRegister extends SQLiteOpenHelper{
private static Context context;
//Database Version
private static final int DATABASE_VERSION=6;
//Database Name
private static final String DATABASE_NAME="event_db";
//Table Names
private static final String TABLE_NAME="event_table";
//Database Constants I am taking some constants as demo you can take your own
private static final String COLUMN_ID="id";
private static final String COLUMN_TITLE="event_title";
private static final String COLUMN_CATEGORY="event_category";
private static final String COLUMN_DATE="event_date";
private static final String COLUMN_TIME="event_time";
private static final String COLUMN_DESCRIPTION="event_description";
//Command to create a Table
private static final String CREATE_TABLE="create table "+TABLE_NAME+"("
+COLUMN_ID+" integer primary key autoincrement,"
+COLUMN_TITLE+" text,"
+COLUMN_CATEGORY+" text,"
+COLUMN_DATE+" text,"
+COLUMN_TIME+" text,"
+COLUMN_DESCRIPTION+" text )";
public EventRegister(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
}
/*
*This method will be called on Start
*/
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
/*
* This method will be called onUpgradate of Database
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
//Method for adding data in database EventData is my Model Object You can insert value as Single or Using Model class also
public void addEvent(EventData taskdata)
{
int flag=0;
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(COLUMN_TITLE, taskdata.getEvent_title());
contentValues.put(COLUMN_CATEGORY, taskdata.getEvent_category());
contentValues.put(COLUMN_DATE, taskdata.getEvent_date());
contentValues.put(COLUMN_TIME, taskdata.getEvent_time());
contentValues.put(COLUMN_DESCRIPTION, taskdata.getEvent_description());
flag=db.insert(TABLE_NAME, null, contentValues);
db.close();
}
//Add Other methods here
从MainActivity或者你想获取数据可以使用someThing,如
EventRegister eventRegister=new EventRegister(context);
eventRegister.addData(EventData);
我希望你有一些指导如何使用SQLite
答案 3 :(得分:0)
对于db操作,我使用这样的辅助类:
public class DBAdapter {
private static final String DATABASE_NAME = "DBNAME";
private static final int DATABASE_VERSION = 1;
private static DBAdapter sInstance;
private final Context mContext;
public DatabaseHelper DBHelper;
public SQLiteDatabase mDb;
private DBAdapter(Context ctx) {
this.mContext = ctx.getApplicationContext();
DBHelper = new DatabaseHelper(mContext);
}
public static DBAdapter getInstance(Context context) {
if (sInstance == null) {
sInstance = new DBAdapter(context.getApplicationContext());
}
return sInstance;
}
public static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
...
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
...
}
}
//---opens the database---
public DBAdapter open() throws SQLException {
mDb = DBHelper.getWritableDatabase();
return this;
}
//add any db functions you want here
public Cursor getCursor(int var) {
...do stuff here with the mDb database
}
这使用单例模式,因此始终只使用与db的一个连接。
从活动中,我像这样访问数据库:
DBAdapter mDb = DBAdapter.getInstance(getApplicationContext());
mDb.open();
Cursor cur= mDb.getCursor(2);
您不需要关闭数据库,因为只使用一个连接,当应用程序关闭时它将关闭,但您也可以在open()函数之后添加此连接
public void close() {
DBHelper.close();
}