Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未创建Android SQLite数据库_Java_Android_Database_Sqlite - Fatal编程技术网

Java 未创建Android SQLite数据库

Java 未创建Android SQLite数据库,java,android,database,sqlite,Java,Android,Database,Sqlite,未创建SQLite数据库。在过去的几个小时里我一直在玩这个,但现在我累了很多。我在日志Cat中没有收到任何错误或警告。这是我的代码,到目前为止我已经尝试过了。我们将不胜感激 public class MySQLiteHelper extends SQLiteOpenHelper{ final static String DB_NAME = "citiesdata.db"; final static String TABLE_NAME = "allcities"; fina

未创建SQLite数据库。在过去的几个小时里我一直在玩这个,但现在我累了很多。我在
日志Cat
中没有收到任何错误或警告。这是我的代码,到目前为止我已经尝试过了。我们将不胜感激

public class MySQLiteHelper extends SQLiteOpenHelper{
    final static String DB_NAME = "citiesdata.db";
    final static String TABLE_NAME = "allcities";
    final static String ID = "_id";
    final static String CITY_CODE = "city_code";
    final static int DB_VERSION = 1;
    private SQLiteDatabase db;
    Context cont;

    public MySQLiteHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        cont = context;
        Toast.makeText(cont, "constructor called", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Toast.makeText(cont, "onCreate method called", Toast.LENGTH_LONG).show();
        String createTable = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + CITY_CODE + " TEXT);";
        db.execSQL(createTable);

//      INSERTING RECORDS IN DATABASE
        SQLiteDatabase writableDB = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CITY_CODE, "333");
        writableDB.insert(TABLE_NAME, null, values);
        writableDB.close();
        readCity();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Toast.makeText(cont, "onUpgrade method called", Toast.LENGTH_LONG).show();
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }


    public void readCity(){
        Toast.makeText(cont, "readCity method called", Toast.LENGTH_LONG).show();
        String selectQuery = "SELECT  * FROM " + TABLE_NAME;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        Toast.makeText(cont, cursor.getString(cursor.getColumnIndex(CITY_CODE)), Toast.LENGTH_LONG).show();
        db.close();
    }
}
在主UI线程中调用构造函数的按钮点击如下

new MySQLiteHelper(MainActivity.this);
public void fillTable(){
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CITY_CODE, "333");
    db.insert(TABLE_NAME, null, values);
    db.close();
    readCity();
}
权限是

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>


事先非常感谢。

最后,我直接把问题解决了。但是怎么做呢

SQLiteOpenHelper
的逻辑是,它不会仅通过创建对象来调用
onCreate
方法

  • 每当您试图访问数据库以检索或插入数据时,如果数据库尚未创建/可用,它将首次调用
    onCreate
    方法
  • 如果数据库可用,但版本低于您提供的版本,则将调用
    onUpgrade
    方法
  • 我在我的
    MySQLiteHelper
    类中创建了一个
    fillTable
    方法,通过调用
    SQLiteOpenHelper
    getwritedatabase
    方法,我第一次尝试访问数据库进行编写。
    fillTable
    方法的定义如下所示

    new MySQLiteHelper(MainActivity.this);
    
    public void fillTable(){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(CITY_CODE, "333");
        db.insert(TABLE_NAME, null, values);
        db.close();
        readCity();
    }
    
    在类的新实例上调用fillTable方法:

    new MySQLiteHelper(MainActivity.this).fillTable();
    
    感谢所有在评论中帮助我的人

    public MySQLiteHelper(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        cont = context;
        SQLiteDatabase db = this.getReadableDatabase();
        Toast.makeText(cont, "constructor called", Toast.LENGTH_LONG).show();
    }
    

    更改构造函数代码,然后它就会工作…

    请显示更多初始化代码
    MySQLiteHelper
    确切的问题是什么?
    readCity()
    是否失败?