SQLite之SQLiteStatement

SQLite之SQLiteStatement,第1张

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

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

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

新建一个数据库和users表:


?
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; @OverrIDe protected 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条数据的时候,并没用使用异步类或新的线程.我发现界面明显会出现停顿的现象.如果插入更大数量的数据时,会直接停止响应. 所以,在进行数据 *** 作的时候,如果数据量较大,建议使用异步类或开启新的线程.

总结

以上是内存溢出为你收集整理的SQLite之SQLiteStatement全部内容,希望文章能够帮你解决SQLite之SQLiteStatement所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/1170283.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-02
下一篇 2022-06-02

发表评论

登录后才能评论

评论列表(0条)