要解决“Android数据库文件未被创建”的问题,可以按照以下步骤进行操作:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "my_database";
private static final int DATABASE_VERSION = 1;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String createTableQuery = "CREATE TABLE my_table (id INTEGER PRIMARY KEY, name TEXT)";
db.execSQL(createTableQuery);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String dropTableQuery = "DROP TABLE IF EXISTS my_table";
db.execSQL(dropTableQuery);
onCreate(db);
}
}
public class MainActivity extends AppCompatActivity {
private DatabaseHelper databaseHelper;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHelper(this);
database = databaseHelper.getWritableDatabase();
}
}
通过以上步骤,你可以解决“Android数据库文件未被创建”的问题,并使用创建的数据库进行操作。
上一篇:android数据库索引
下一篇:android数据库修改