Android 判断SQLite数据库中是否存在某一个表格

final String CREATE_BASE_TABLE ="create table if not existslogin (" + "id INTEGER PRIMARY KEY,"+ "email TEXT,"+ "passwordTEXT,"+ ");";

I have an android app that needs to check if there’s already adatabase available in the device, and if not, process some thingsand eventually create it.Then check if a particular tableexists.

SQLiteDatabase db; db = openOrCreateDatabase("TestData.db",SQLiteDatabase.CREATE_IF_NECESSARY , null);

If database exists and table exists simply read the data fromthe database if the data does exist.

Tip: For checking if a table Exists:

However, It is easy to find if a table exists or NOT,

Create a SQLiteDatabase object and have a call to query(…), thesql query string has select command. If the returned cursor is nullthen the table doesn’t exist.

SQLiteDatabase tempDatabase; try {tempDatabase=SQLiteDatabase.openDatabase(DBPATH+DBNAME, null,SQLiteDatabase.OPEN_READONLY);try{Cursor cur;cur=tempDatabase.rawQuery("select * from table whereid='"+idvar+";",null); if(cur==null){//our table doesn't exist, sowe'll create one or take an action.} }catch (SQLiteException e) {//our table doesn't exist, so we'll create one or take an action.}}catch (SQLiteException e) { //our database doesn't exist, so we'llcreate one or take an action.}

翻译过来大致是这个意思:

有两种方法,

第一种方法是:不需要知道表是否存在,在创建表的时候加上if not exists 例:create table if notexists myTable(...),这样做的好处是,不需要知道表是否存在,只要每次都进行创建即可。因为里面有判断,如果表存在的时候,再执行这句不会发生重复创建表的情况。

第二种方法是:直接执行某个SQL语句,用try...catch来捕获数据库操作的异常。当异常表示数据库表不存在的时候,再进行处理。例:

try{

Cursor c = getWritableDatabase().query("select * frommyTable",null );

catch(Exception e) // 或者是 SQLiteException .

{//添加处理代码,例如:创建表。

}

第二种的方法不如第一种好,原因是:第二种写起来比较麻烦,还有,如果有其它异常的时候,需要对异常来进行判别,并处理。

PS:

上面的E文中有一种方法是判断query的返回值,那个别想了,我在测试的时候,如果表被删除了,一到那里就崩溃,只能通过try...catch的方法。

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Studio 创建 SQLite 数据库表格需要以下步骤: 1. 在项目的 `app` 目录下的 `java` 文件夹创建一个新的 `DatabaseHelper` 类,继承自 `SQLiteOpenHelper` 类。 ```java public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "mydatabase.db"; 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 CREATE_TABLE = "CREATE TABLE mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)"; db.execSQL(CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String DROP_TABLE = "DROP TABLE IF EXISTS mytable"; db.execSQL(DROP_TABLE); onCreate(db); } } ``` 2. 在 `onCreate()` 方法创建表格,这里创建了一个名为 `mytable` 的表格,包含三个字段:`id`、`name` 和 `age`。 ```java String CREATE_TABLE = "CREATE TABLE mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)"; db.execSQL(CREATE_TABLE); ``` 3. 在 `onUpgrade()` 方法更新表格,这里先删除了旧的表格,再重新创建一个新的表格。 ```java String DROP_TABLE = "DROP TABLE IF EXISTS mytable"; db.execSQL(DROP_TABLE); onCreate(db); ``` 4. 在需要使用数据库的地方,创建一个 `DatabaseHelper` 对象,并获取一个可写的数据库实例。 ```java DatabaseHelper dbHelper = new DatabaseHelper(context); SQLiteDatabase db = dbHelper.getWritableDatabase(); ``` 5. 在获取到数据库实例后,就可以使用各种 SQL 语句对数据进行操作了,例如插入数据、查询数据、更新数据、删除数据等等。 ```java // 插入数据 ContentValues values = new ContentValues(); values.put("name", "Tom"); values.put("age", 20); db.insert("mytable", null, values); // 查询数据 Cursor cursor = db.query("mytable", null, null, null, null, null, null); while (cursor.moveToNext()) { int id = cursor.getInt(cursor.getColumnIndex("id")); String name = cursor.getString(cursor.getColumnIndex("name")); int age = cursor.getInt(cursor.getColumnIndex("age")); Log.d("TAG", "id: " + id + ", name: " + name + ", age: " + age); } // 更新数据 ContentValues values = new ContentValues(); values.put("age", 21); db.update("mytable", values, "name = ?", new String[]{"Tom"}); // 删除数据 db.delete("mytable", "age < ?", new String[]{"20"}); ``` 以上就是在 Android Studio 创建 SQLite 数据库表格的基本步骤。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值