加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

SQLite之SQLiteStatement

发布时间:2020-12-12 19:44:26 所属栏目:百科 来源:网络整理
导读:平常在做Android数据库操作时,都是用的 execSQL 之个方法. 今天偶然发现了 SQLiteStatement 这个类.让我想起了在做Java Web开发写JDBC的代码时Prestatement这个类.Prestatement不仅提高了效率,也解决了SQL注入的问题.那在Android中的SQLiteStatement,是否也
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class DBHelper extends SQLiteOpenHelper {private static final String DB_NAME = "userdb"; final int DB_VERSION = 1;public DBHelper(Context context) {super(context,DB_NAME,null,DB_VERSION);}@Override void onCreate(SQLiteDatabase db) {StringBuffer sql = new StringBuffer();sql.append("create table users");"(_id int PRIMARY KEY,name varchar,gender int,age int,phoneNumber varchar,address varchar)");db.execSQL(sql.toString());}@Override onUpgrade(SQLiteDatabase db,153)!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-weight:bold!important; font-size:1em!important; min-height:inherit!important">int oldVersion,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important"> newVersion) {}}然后分别使用SQLiteDatabase 的execSQL方法和SQLiteStatement的executeInsert方法进入插入,比较执行所需要的时间:


? 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 UserDao { private DBHelper dbHelper; SQLiteDatabase db; StringBuffer sql_insert; List<User> users; UserDao(Context context){ this .dbHelper = DBHelper(context); .db = dbHelper.getWritableDatabase(); sql_insert = StringBuffer(); sql_insert.append( "INSERT INTO users(name,gender,age,phoneNumber,address) " " VALUES( ?,?,?)" ); users = ArrayList<User>(); //测试数据 for ( i = 0 ;i< 1000 ;i++){ User user = User(); user.setId(i); user.setName( "name" +i); user.setGender( ); user.setAge(user.getRandomAge()); user.setPhoneNumber( "13800138000" ); user.setAddress( "GuangDong ShenZhen No." +i); users.add(user); } } /** * 使用SQLiteDatabase 的execSQL方法插入数据 * @return 返回执行所需要的时间 */ long insertexecSQL() { long start=System.currentTimeMillis(); (User user:users){ Object[] bindArgs = {user.getName(),user.getGender(),user.getAge(),user.getPhoneNumber(),user.getAddress()}; db.execSQL(sql_insert.toString(),bindArgs); } end = System.currentTimeMillis(); return end - start; } /** * 使用SQLiteStatement的executeInsert方法插入数据 * @return 返回执行所需要的时间 */ insertStatement() { start = System.currentTimeMillis(); (User user:users){ SQLiteStatement statement= db.compileStatement(sql_insert.toString()); statement.bindString( statement.bindLong( 2 3 4 5 statement.executeInsert(); } end = System.currentTimeMillis(); end - start; } }

界面方面就两个按钮,分别调用不同的插入方法,并将执行所需的时间显示在Button上.

? 33 MainActivity Activity { Button btn1; Button btn2;@Overrideprotected onCreate(Bundle savedInstanceState) {.onCreate(savedInstanceState);setContentView(R.layout.activity_main);final UserDao dao = UserDao();btn1 = (Button) findViewById(R.id.btn1);btn2 = (Button) findViewById(R.id.btn2);btn1.setOnClickListener( OnClickListener() {@Override onClick(View v) {btn1.setText(String.valueOf(dao.insertexecSQL()));}});btn2.setOnClickListener( OnClickListener() {@Override onClick(View v) {btn2.setText(String.valueOf(dao.insertStatement()));}});}

通过几次比较发现,插入1000条数据,使用SQLiteStatement的executeInsert方法一般比使用SQLiteDatabase 的execSQL方法快5秒左右.这个差距还是很大的.

需要说明的是,上面的两个方法我们都没有开启事物.在进行这样的批量操作时,开启事物肯定会很大程度上提高效率.

db.beginTransaction();
xxxx….
db.setTransactionSuccessful();
db.endTransaction();

还有一个需要注意的问题是,在批量插入1000条数据的时候,并没用使用异步类或新的线程.我发现界面明显会出现停顿的现象.如果插入更大数量的数据时,会直接停止响应. 所以,在进行数据操作的时候,如果数据量较大,建议使用异步类或开启新的线程.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

平常在做Android数据库操作时,都是用的execSQL之个方法. 今天偶然发现了SQLiteStatement这个类.让我想起了在做Java Web开发写JDBC的代码时Prestatement这个类.Prestatement不仅提高了效率,也解决了SQL注入的问题.那在Android中的SQLiteStatement,是否也会提高一些效率呢?

于是写了一个简单的测试,比较execSQL和SQLiteStatement的executeInsert方法插入1000条数据所需要的时间.都没有使用事物.

新建一个数据库和users表:


?