Android之SQLiteDatabase使用与特殊情况处理



1、打开数据库失败
static SQLiteDatabase openOrCreateDatabase(String path, CursorFactory factory)失败,
错误提示如下:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (Sqlite code 14): Could not open database, (OS error - 2:No such file or directory)
这是因为path的值不对,应该和当前应用程序的包名目录一致,也就是读写权限。
使用下面几种方法:
1.1、contex.getFileDir().toString() + "/" + db_name;
1.2、context.getDatabasePath(db_name).getPath();

 

2、建表失败
String sql = "create table " + TB_NAME + "(_id integer primary key autoincrement,"
                        + " title varchar(50),"
                        + " content varchar(255))";
mDb.execSQL(sql);失败,
错误提示如下:
android.database.sqlite.SQLiteException: table info already exists (Sqlite code 1): , while compiling: create table info(_id integer primary key autoincrement, title varchar(50), content varchar(255)), (OS error - 2:No such file or directory)
数据库表不能重复创建,会出错。
在建表之前可以先删除,再建表,或者表存在,就不需要再创建。
如何判断表是否存在,可以使用以下几种方法:
https://blog.csdn.net/q610098308/article/details/46803689

    /**
     * 判断某张表是否存在
     * @param tabName 表名
     * @return
     */ 
    public boolean tabbleIsExist(String tableName){ 
            boolean result = false; 
            if(tableName == null){ 
                    return false; 
            } 
            SQLiteDatabase db = null; 
            Cursor cursor = null; 
            try { 
                    db = this.getReadableDatabase(); 
                                                           //这里表名可以是Sqlite_master 
                    String sql = "select count(*) as c from "+AppConstant.DataBaseName+" where type ='table' and name ='"+tableName.trim()+"' "; 
                    cursor = db.rawQuery(sql, null); 
                    if(cursor.moveToNext()){ 
                            int count = cursor.getInt(0); 
                            if(count>0){ 
                                    result = true; 
                            } 
                    } 
                     
            } catch (Exception e) { 
                    // TODO: handle exception 
            }                 
            return result; 
    }

https://blog.csdn.net/lizhenmingdirk/article/details/17561471
3种方法,判断表是否存在
1)
可以在创建表之前判断,这样就不会重新创建,

create table if not exists Student(name text primary key, code integer);

比平时多了if not exists
2)

Cursor cursor = db.rawQuery("select name from sqlite_master where type='table';", null);
  while(cursor.moveToNext()){
   //遍历出表名
   String name = cursor.getString(0);
   Log.i("System.out", name);
  }

3)

String sql = "select count(*) as c from sqlite_master where type ='table' and name ='Student';";
                    cursor = db.rawQuery(sql, null);
                    if(cursor.moveToNext()){
                            int count = cursor.getInt(0);
                            if(count>0){
                                    result = true;
                            }
                    }

3、在子线程中进行数据库表的增删改查
Android中不要在主线程做数据库表记录的增删改查,
因为IO操作相对比较耗时,会拖慢UI的响应速度。

 

4、字符串值最好用单引号
字段值,特别是字符串,一定要加单引号,否则sql语句会将空格做截断处理。

String title = "alpha";
String content = "good boy";
String values = String.format("values(%d, '%s', '%s')", mId++, title, content);
String sql = "insert into info " + values;
mDb.execSQL(sql);

5、退出时,需要关闭数据库。


完整代码如下:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final int MSG_VIEW_CONTENT = 0x01;
    private static final String DB_NAME = "sql_test.db";
    private static final String TB_NAME = "info";

    private ListView mLvInfo;
    private InfoAdapter mAdapter;
    private int mId = 0;
    private List<String> mListInfo;


    private SQLiteDatabase mDb = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mLvInfo = (ListView) findViewById(R.id.lv_info);
        mAdapter = new InfoAdapter(MainActivity.this);
        mLvInfo.setAdapter(mAdapter);


        View insert = findViewById(R.id.tv_insert);
        View update = findViewById(R.id.tv_update);
        View query  = findViewById(R.id.tv_query);
        View delete = findViewById(R.id.tv_delete);


        insert.setOnClickListener(this);
        update.setOnClickListener(this);
        query.setOnClickListener(this);
        delete.setOnClickListener(this);


        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                openDataBase();
            }
        }, 500);
    }



    private void openDataBase() {
        String db_name = getDatabasePath(DB_NAME).getPath();
        mDb = SQLiteDatabase.openOrCreateDatabase(db_name, null);
        createTable();
    }



    private boolean isTableExists(String tableName) {
        boolean isExists = false;
        if (mDb != null) {
            String sql_query = "select count(*) as c from Sqlite_master  where type ='table' and name ='" + tableName + "'";
            Cursor cursor = mDb.rawQuery(sql_query, null);
            if (cursor != null) {
                if (cursor.moveToNext()) {
                    int count = cursor.getInt(0);
                    if (count > 0) {
                        isExists = true;
                    }
                }
            }
        }
        return isExists;
    }



    private void createTable() {
        if (mDb != null) {
            if (!isTableExists(TB_NAME)) {
                String sql = "create table " + TB_NAME + "(_id integer primary key autoincrement,"
                        + " title varchar(50),"
                        + " content varchar(255))";
                mDb.execSQL(sql);
            } else {
                queryRecord();
            }
        }
    }



    private void insertRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mDb != null) {
                    String title = "alpha";
                    String content = "good boy";
                    String values = String.format("values(%d, '%s', '%s')", mId++, title, content);
                    String sql = "insert into " + TB_NAME + " " + values;
                    mDb.execSQL(sql);
                }
            }
        }).start();
    }



    private void updateRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mDb != null) {
                    String title = "beta";
                    String update = String.format(" set title = '%s' where _id = %d", title, mId > 0 ? mId-1 : mId);
                    String sql = "update " + TB_NAME + update;
                    mDb.execSQL(sql);
                }
            }
        }).start();
    }



    private void queryRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mDb != null) {
                    String sql = "select * from " + TB_NAME;
                    Cursor cursor = mDb.rawQuery(sql, null);
                    if (cursor != null) {
                        if (mListInfo != null) {
                            mListInfo.clear();
                        }
                        while (cursor.moveToNext()) {
                            int _id = cursor.getInt(0);
                            String title = cursor.getString(1);
                            String content = cursor.getString(2);
                            if (mListInfo == null) {
                                mListInfo = new ArrayList<String>();
                            }
                            mId = _id + 1;
                            mListInfo.add("_id:" + _id + ",title:" + title + ",content:" + content);
                        }
                        mHandler.sendEmptyMessage(MSG_VIEW_CONTENT);
                    }
                }



            }
        }).start();
    }



    private void deleteRecord() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mDb != null) {
                    int _id = mId > 0 ? mId-1 : mId;
                    String delete = String.format(" where _id = %d", _id);
                    String sql = "delete from " + TB_NAME + delete;
                    mDb.execSQL(sql);
                }
            }
        }).start();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_insert:
                insertRecord();
                break;
            case R.id.tv_update:
                updateRecord();
                break;
            case R.id.tv_query:
                queryRecord();
                break;
            case R.id.tv_delete:
                deleteRecord();
                break;
            default:
                break;
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mDb != null && mDb.isOpen()) {
            mDb.close();
        }
    }



    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_VIEW_CONTENT:
                    if (mAdapter != null) {
                        mAdapter.notifyDataSetChanged();
                    }
                    break;
                default:
                    break;
            }
        }
    };



    class InfoAdapter extends BaseAdapter {
        private Context mContext;
        private LayoutInflater mInflater;



        public InfoAdapter(Context context) {
            mContext = context;
            mInflater = LayoutInflater.from(mContext);
        }
        @Override
        public int getCount() {
            if (mListInfo != null) {
                return mListInfo.size();
            }
            return 0;
        }



        @Override
        public Object getItem(int position) {
            if (mListInfo != null) {
                return mListInfo.get(position);
            }
            return null;
        }



        @Override
        public long getItemId(int position) {
            return 0;
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_info, parent, false);
            }



            TextView tvContent = (TextView) convertView.findViewById(R.id.tv_content);
            String content = (String) getItem(position);
            tvContent.setText(content);

            return convertView;
        }
    }
}

 

Android中,使用SQLite数据库更新多行数据的操作通常涉及以下几个步骤: 1. **打开数据库连接**: 首先,你需要通过`SQLiteDatabase.openDatabase()`或者`SQLiteOpenHelper.getWritableDatabase()`方法打开一个 writable 数据库连接。 2. **创建更新查询**: 对于每一条需要增加的数据,你可以编写一个UPDATE SQL语句。例如,假设你想将表中所有名为`column_name`的列值加1,可以这样做: ```sql UPDATE table_name SET column_name = column_name + 1; ``` 其中 `table_name` 是你的表名,`column_name` 是你要增加的字段名。 3. **批量执行更新操作**: AndroidSQLiteDatabase提供了`execSQL()`方法或`rawQuery()`方法执行SQL命令。如果你有一批数据需要同时更新,最好批量处理,避免频繁的交互导致性能下降。这可以通过循环遍历每一项数据,并构造对应的SQL语句,然后一次性执行。 4. **事务管理**(可选): 如果有多步操作关联在一起,为了保证数据的一致性,你可以使用事务(`startTransaction()`、`setTransactionSuccessful()` 和 `endTransaction()`)。这样如果更新过程中发生错误,整个事务会被回滚。 5. **关闭连接**: 更新完成后,别忘了关闭数据库连接,释放资源。 ```java // 假设cursor是查询结果,包含待更新的数据 while (cursor.moveToNext()) { String id = cursor.getString(cursor.getColumnIndex("id")); int currentValue = cursor.getInt(cursor.getColumnIndex("column_name")); // 构造SQL语句 String updateSql = "UPDATE table_name SET column_name = column_name + 1 WHERE id = ?"; // 执行更新 SQLiteDatabase db = ...; // 获取数据库实例 db.execSQL(updateSql, new Object[]{id}); } // 关闭游标和数据库连接 cursor.close(); db.close(); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值