不是SQLite的开放助手创建SQLite数据库助手、数据库、不是、SQLite

2023-09-04 03:04:28 作者:发尐呆。

我在我的Andr​​oid应用程序中创建这个类。但是,当我要检索创建的数据库中的数据,它显示数据库中不可用。请帮我做这件事。谢谢

 公共类数据库处理器扩展SQLiteOpenHelper {

    私有静态最后弦乐DATABASE_NAME =dbumbers;
      私有静态最终诠释DATABASE_VERSION = 1;

      私有静态最后弦乐TABLE_NAME =数字;

    公共数据库处理器(上下文的背景下,字符串名称,CursorFactory厂,
            INT版){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);

    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        //创建数据库查询
        db.execSQL(CREATE TABLE+ TABLE_NAME +(区文本,类文本,说明文字,号码文本););

        //插入查询
        db.execSQL(插入+ TABLE_NAME +值(安帕拉,医院,安帕赖总医院,0112456852););
        db.execSQL(插入+ TABLE_NAME +值(阿努拉德普勒,酒吧,阿努拉德普勒酒吧,011245852););
        db.execSQL(插入+ TABLE_NAME +值(科伦坡,零售,科伦坡超市,0112546852););
        db.execSQL(插入+ TABLE_NAME +值(加姆珀哈,飞行,加姆珀哈机场,0112455552););

    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        //添加更多的插入查询,如果您需要在以后添加更多的DATAS,但你必须先您DATABASE_VERSION升级到更高一些

    }

}
 

这是取出部分

 保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_data_check);

        SQLiteDatabase DB = openOrCreateDatabase(dbumbers,1,NULL);

        光标C = db.rawQuery(SELECT * FROM号,NULL);

        //c.moveToFirst();
        而(c.moveToNext()){
            字符串区= c.getString(0);
            字符串类别= c.getString(1);
            字符串描述= c.getString(2);
            串号= c.getString(2);

            串排=小区+ - +分类+ - +说明书+ - +数字;

            Toast.makeText(这一点,行,Toast.LENGTH_LONG).show();

        }


        db.close();



    }
 

解决方案 如何创建sqlite数据库

有关数据库操作,我使用了一个辅助类是这样的:

 公共类DBAdapter {
私有静态最后弦乐DATABASE_NAME =DBNAME;
私有静态最终诠释DATABASE_VERSION = 1;

私有静态DBAdapter sInstance;

私人最终语境mContext;

公共DatabaseHelper DBHelper;
公共SQLiteDatabase MDB;

私人DBAdapter(上下文CTX){
    this.mContext = ctx.getApplicationContext();
    DBHelper =新DatabaseHelper(mContext);
}

公共静态DBAdapter的getInstance(上下文的背景下){
    如果(sInstance == NULL){
      sInstance =新DBAdapter(context.getApplicationContext());
    }
    返回sInstance;
  }

公共静态类DatabaseHelper扩展SQLiteOpenHelper {
    DatabaseHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }

    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        ...
    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        ...
    }



 }

// ---打开数据库---
公共DBAdapter的open()抛出的SQLException {
    MDB = DBHelper.getWritableDatabase();
    回到这一点;
}
//添加你想在这里的任何数据库的功能


 公共光标getCursor(INT VAR){
    ...做的东西在这里与MDB数据库
}
 

本使用这样只有一个连接到数据库是用来在任何时候都一个单例模式。

这是一个活动,我访问数据库是这样的:

  DBAdapter MDB = DBAdapter.getInstance(getApplicationContext());
mDb.open();
    光标CUR = mDb.getCursor(2);
 

您不需要关闭数据库,因为只有一个连接将被使用,在应用程序关闭时,它会被关闭,但你也可以在open()函数后添加此

 公共无效的close(){
    DBHelper.close();
}
 

I have created this class in my android application. But when I'm going to retrieve data from created database, it's showing the database not available. Please help me to do this. Thanks

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

    }

}

this is retrieval part

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();



    }

解决方案

For db operations, I use a helper class like this:

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
}

This uses a singleton pattern so only one connection to the db is used at all times.

From an activity, I access the db like this:

DBAdapter mDb = DBAdapter.getInstance(getApplicationContext());  
mDb.open();
    Cursor cur= mDb.getCursor(2);

You dont need to close the db as only one connection will be used, it will be closed when the app closes but you could also add this after the open() function

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