Android对SQLite批量添加数据

[+]

有人去面试的时候面试官问这么一个问题。

如何将大量的数据同时插入到Sqlite?或者说批量数据插入数据库?

本人总结了一下几种方法,重点注意后面那一点

1. 使用ContentValues插入

db.beginTransaction(); // 手动设置开始事务
        for (ContentValues v : list) {
            db.insert("bus_line_station", null, v);
        }
        db.setTransactionSuccessful(); // 设置事务处理成功,不设置会自动回滚不提交
        db.endTransaction(); // 处理完成

        db.close();

这里是把要插入的数据封装到ContentValues类中,然后调用db.isert()方法执行插入

2. 使用db.execSQL(sql)

这里是把要插入的数据拼接成可执行的sql语句,然后调用db.execSQL(sql)方法执行插入

public void inertOrUpdateDateBatch(List<String> sqls) {   
        SQLiteDatabase db = getWritableDatabase();   
        db.beginTransaction();   
        try {   
            for (String sql : sqls) {   
                db.execSQL(sql);   
            }   
            // 设置事务标志为成功,当结束事务时就会提交事务   
            db.setTransactionSuccessful();   
        } catch (Exception e) {   
            e.printStackTrace();   
        } finally {   
            // 结束事务   
            db.endTransaction();   
            db.close();   
        }   

} 


3. 使用InsertHelper类

InsertHelper ih = new InsertHelper(db, "bus_line_station");
        db.beginTransaction();
        final int directColumnIndex = ih.getColumnIndex("direct");
        final int lineNameColumnIndex = ih.getColumnIndex("line_name");
        final int snoColumnIndex = ih.getColumnIndex("sno");
        final int stationNameColumnIndex = ih.getColumnIndex("station_name");
        try {
            for (Station s : busLines) {
                ih.prepareForInsert();
                ih.bind(directColumnIndex, s.direct);
                ih.bind(lineNameColumnIndex, s.lineName);
                ih.bind(snoColumnIndex, s.sno);
                ih.bind(stationNameColumnIndex, s.stationName);
                ih.execute();
            }
            db.setTransactionSuccessful();
        } finally {
            ih.close();
            db.endTransaction();
            db.close();
        }


4. 使用SQLiteStatement插入 这种效率高 速度快 也是面试官考你的重点就是这个

String sql = "insert into bus_line_station(direct,line_name,sno,station_name) values(?,?,?,?)";
        SQLiteStatement stat = db.compileStatement(sql);
        db.beginTransaction();
        for (Station line : busLines) {
            stat.bindLong(1, line.direct);
            stat.bindString(2, line.lineName);
            stat.bindLong(3, line.sno);
            stat.bindString(4, line.stationName);
            stat.executeInsert();
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        db.close();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Python中使用SQLite进行批量插入数据可以通过以下步骤实现: 1. 首先,确保已经安装了Python的SQLite模块,可以使用`pip install sqlite3`命令进行安装。 2. 导入SQLite模块:`import sqlite3` 3. 连接到SQLite数据库:`conn = sqlite3.connect('database.db')`,其中'database.db'是数据库文件的名称,如果不存在则会创建一个新的数据库文件。 4. 创建一个游标对象:`cursor = conn.cursor()` 5. 定义插入数据的SQL语句:`sql = "INSERT INTO table_name (column1, column2, ...) VALUES (?, ?, ...)"`,其中table_name是表名,column1、column2等是表的列名。 6. 准备要插入的数据,以列表的形式存储:`data = [(value1, value2, ...), (value1, value2, ...), ...]` 7. 执行批量插入操作:`cursor.executemany(sql, data)` 8. 提交事务:`conn.commit()` 9. 关闭游标和数据库连接:`cursor.close()`和`conn.close()` 下面是一个示例代码: ```python import sqlite3 # 连接到SQLite数据库 conn = sqlite3.connect('database.db') cursor = conn.cursor() # 定义插入数据的SQL语句 sql = "INSERT INTO students (name, age) VALUES (?, ?)" # 准备要插入的数据 data = [('Alice', 20), ('Bob', 22), ('Charlie', 21)] # 执行批量插入操作 cursor.executemany(sql, data) # 提交事务 conn.commit() # 关闭游标和数据库连接 cursor.close() conn.close() ``` 请注意,上述示例中的表名为`students`,列名为`name`和`age`,你需要根据自己的数据库表结构进行相应的修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值