码迷,mamicode.com
首页 > 移动开发 > 详细

Android Sqlite数据库执行插入查询更新删除的操作对比

时间:2015-08-07 19:09:10      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

下面是在Android4.0上,利用Sqlite数据库的insert,query,update,delete函数以及execSql,rawQuery函数执行插入,查询,更新,删除操作花费时间的对比结果。

是在执行相同的动作,记录条数也一样的情况下的对比,多次验证的结果是:

(1)如果批量执行的记录数在1000条,则Android SqliteDatabase提供的insert,query,update,delete函数和直接写SQL文的execSql,rawQuery的效率差不多,几乎一样。所以使用哪种放到都可以,不会影响到执行效率。

(2)如果批量执行的记录数在10万条,则会存在差别。在某台手机上SqliteDatabase提供的insert执行插入操作耗时45秒,要比execSql插入35秒慢10秒左右。

可见在数据库大的情况下,还是有差别的。execSql省去了拼接sql语句的步骤,要比SqliteDatabase提供的insert,query,update,delete等函数效率高。当数据库越大,差别也越大。

下面是验证代码:

 

[java] view plaincopy
 
  1. public class MainActivity extends Activity {  
  2.     private static final String TAG = "MainActivity";  
  3.       
  4.     private DBHelper mDbHelper = null;  
  5.     private static TextView mTvInfo;  
  6.     private static ProgressDialog mDialog = null;  
  7.       
  8.     private static Handler mHandler = new Handler() {  
  9.         @Override  
  10.         public void handleMessage(Message msg) {  
  11.             // TODO Auto-generated method stub  
  12.             if(mDialog != null) {  
  13.                 mDialog.dismiss();  
  14.                 mDialog = null;  
  15.             }  
  16.             if (msg.what == 0) {  
  17.                 Bundle b = msg.getData();  
  18.                 int seconds = b.getInt("seconds");  
  19.                 int ms = b.getInt("ms");  
  20.                 StringBuilder builder = new StringBuilder("insert插入1000条记录花费时间:");  
  21.                 if (seconds > 0) {  
  22.                     builder.append(seconds).append("s ");  
  23.                 }  
  24.                 builder.append(ms).append("ms");  
  25.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  26.                 // 最多显示8条信息  
  27.                 for (int i = 0; i < strLines.length; i++) {  
  28.                     builder.append(‘\n‘).append(strLines[i]);  
  29.                     if (i == 6) {  
  30.                         break; // 加上原来最多7条信息,共显示8条信息  
  31.                     }  
  32.                 }  
  33.                 mTvInfo.setText(builder.toString());  
  34.             } else if (msg.what == 1) {  
  35.                 Bundle b = msg.getData();  
  36.                 int seconds = b.getInt("seconds");  
  37.                 int ms = b.getInt("ms");  
  38.                 StringBuilder builder = new StringBuilder("execSql插入1000条记录花费时间:");  
  39.                 if (seconds > 0) {  
  40.                     builder.append(seconds).append("s ");  
  41.                 }  
  42.                 builder.append(ms).append("ms");  
  43.                   
  44.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  45.                 // 最多显示8条信息  
  46.                 for (int i = 0; i < strLines.length; i++) {  
  47.                     builder.append(‘\n‘).append(strLines[i]);  
  48.                     if (i == 6) {  
  49.                         break; // 加上原来最多7条信息,共显示8条信息  
  50.                     }  
  51.                 }  
  52.                 mTvInfo.setText(builder.toString());  
  53.             } else if (msg.what == 10) {  
  54.                 Bundle b = msg.getData();  
  55.                 int count = b.getInt("count");  
  56.                 int seconds = b.getInt("seconds");  
  57.                 int ms = b.getInt("ms");  
  58.                 StringBuilder builder = new StringBuilder("query查询");  
  59.                 builder.append(count).append("条记录花费时间:");  
  60.                 if (seconds > 0) {  
  61.                     builder.append(seconds).append("s ");  
  62.                 }  
  63.                 builder.append(ms).append("ms");  
  64.                   
  65.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  66.                 // 最多显示8条信息  
  67.                 for (int i = 0; i < strLines.length; i++) {  
  68.                     builder.append(‘\n‘).append(strLines[i]);  
  69.                     if (i == 6) {  
  70.                         break; // 加上原来最多7条信息,共显示8条信息  
  71.                     }  
  72.                 }  
  73.                 mTvInfo.setText(builder.toString());  
  74.             } else if (msg.what == 11) {  
  75.                 Bundle b = msg.getData();  
  76.                 int count = b.getInt("count");  
  77.                 int seconds = b.getInt("seconds");  
  78.                 int ms = b.getInt("ms");  
  79.                 StringBuilder builder = new StringBuilder("rawQuery查询");  
  80.                 builder.append(count).append("条记录花费时间:");  
  81.                 if (seconds > 0) {  
  82.                     builder.append(seconds).append("s ");  
  83.                 }  
  84.                 builder.append(ms).append("ms");  
  85.                   
  86.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  87.                 // 最多显示8条信息  
  88.                 for (int i = 0; i < strLines.length; i++) {  
  89.                     builder.append(‘\n‘).append(strLines[i]);  
  90.                     if (i == 6) {  
  91.                         break; // 加上原来最多7条信息,共显示8条信息  
  92.                     }  
  93.                 }  
  94.                 mTvInfo.setText(builder.toString());  
  95.             } else if (msg.what == 20) {  
  96.                 Bundle b = msg.getData();  
  97.                 int count = b.getInt("count");  
  98.                 int seconds = b.getInt("seconds");  
  99.                 int ms = b.getInt("ms");  
  100.                 StringBuilder builder = new StringBuilder("update更新");  
  101.                 builder.append(count).append("条记录花费时间:");  
  102.                 if (seconds > 0) {  
  103.                     builder.append(seconds).append("s ");  
  104.                 }  
  105.                 builder.append(ms).append("ms");  
  106.                   
  107.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  108.                 // 最多显示8条信息  
  109.                 for (int i = 0; i < strLines.length; i++) {  
  110.                     builder.append(‘\n‘).append(strLines[i]);  
  111.                     if (i == 6) {  
  112.                         break; // 加上原来最多7条信息,共显示8条信息  
  113.                     }  
  114.                 }  
  115.                 mTvInfo.setText(builder.toString());  
  116.             } else if (msg.what == 21) {  
  117.                 Bundle b = msg.getData();  
  118.                 int seconds = b.getInt("seconds");  
  119.                 int ms = b.getInt("ms");  
  120.                 StringBuilder builder = new StringBuilder("execSql更新1000条记录花费时间:");  
  121.                 if (seconds > 0) {  
  122.                     builder.append(seconds).append("s ");  
  123.                 }  
  124.                 builder.append(ms).append("ms");  
  125.                   
  126.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  127.                 // 最多显示8条信息  
  128.                 for (int i = 0; i < strLines.length; i++) {  
  129.                     builder.append(‘\n‘).append(strLines[i]);  
  130.                     if (i == 6) {  
  131.                         break; // 加上原来最多7条信息,共显示8条信息  
  132.                     }  
  133.                 }  
  134.                 mTvInfo.setText(builder.toString());  
  135.             } else if (msg.what == 30) {  
  136.                 Bundle b = msg.getData();  
  137.                 int count = b.getInt("count");  
  138.                 int seconds = b.getInt("seconds");  
  139.                 int ms = b.getInt("ms");  
  140.                 StringBuilder builder = new StringBuilder("delete删除");  
  141.                 builder.append(count).append("条记录花费时间:");  
  142.                 if (seconds > 0) {  
  143.                     builder.append(seconds).append("s ");  
  144.                 }  
  145.                 builder.append(ms).append("ms");  
  146.                   
  147.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  148.                 // 最多显示8条信息  
  149.                 for (int i = 0; i < strLines.length; i++) {  
  150.                     builder.append(‘\n‘).append(strLines[i]);  
  151.                     if (i == 6) {  
  152.                         break; // 加上原来最多7条信息,共显示8条信息  
  153.                     }  
  154.                 }  
  155.                 mTvInfo.setText(builder.toString());  
  156.             } else if (msg.what == 31) {  
  157.                 Bundle b = msg.getData();  
  158.                 int seconds = b.getInt("seconds");  
  159.                 int ms = b.getInt("ms");  
  160.                 StringBuilder builder = new StringBuilder("execSql删除1000条记录花费时间:");  
  161.                 if (seconds > 0) {  
  162.                     builder.append(seconds).append("s ");  
  163.                 }  
  164.                 builder.append(ms).append("ms");  
  165.                   
  166.                 String strLines[] = mTvInfo.getText().toString().split("\n");  
  167.                 // 最多显示8条信息  
  168.                 for (int i = 0; i < strLines.length; i++) {  
  169.                     builder.append(‘\n‘).append(strLines[i]);  
  170.                     if (i == 6) {  
  171.                         break; // 加上原来最多7条信息,共显示8条信息  
  172.                     }  
  173.                 }  
  174.                 mTvInfo.setText(builder.toString());  
  175.             }  
  176.               
  177.               
  178.         }  
  179.     };  
  180.           
  181.     @Override  
  182.     public void onCreate(Bundle savedInstanceState) {  
  183.         super.onCreate(savedInstanceState);  
  184.         setContentView(R.layout.activity_main);  
  185.           
  186.         Button btnCreateDb = (Button)findViewById(R.id.btnCreateDb);  
  187.         btnCreateDb.setOnClickListener(new OnClickListener() {  
  188.               
  189.             @Override  
  190.             public void onClick(View arg0) {  
  191.                 // TODO Auto-generated method stub  
  192.                 mDbHelper = new DBHelper(MainActivity.this, "test", null, 1);  
  193.             }  
  194.               
  195.         });  
  196.           
  197.         // insert插入  
  198.         Button btnInsert = (Button)findViewById(R.id.btnInsert);  
  199.         btnInsert.setOnClickListener(new OnClickListener() {  
  200.               
  201.             @Override  
  202.             public void onClick(View arg0) {  
  203.                 // TODO Auto-generated method stub  
  204.                 if (mDbHelper != null) {  
  205.                     mDialog = ProgressDialog.show(MainActivity.this, "insert插入", "正在插入1000条记录,请稍等……", true);  
  206.                     new Thread() { // 开启线程执行防止阻塞  
  207.                         @Override  
  208.                         public void run() {  
  209.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  210.                             try {  
  211.                                 // 利用事物批量插入数据以提高效率  
  212.                                 ContentValues values = new ContentValues();  
  213.                                 long startms = System.currentTimeMillis();  
  214.                                 db.beginTransaction();  
  215.                                 for (int i = 0; i < 1000; i++) {  
  216.                                     values.put("name", new String("张三") + i);  
  217.                                     values.put("birthday", "2012-12-08");  
  218.                                     values.put("gender", i%2);  
  219.                                     db.insert("student", null, values);  
  220.                                 }  
  221.                                 db.setTransactionSuccessful();  
  222.                                 db.endTransaction();  
  223.                                 long endms = System.currentTimeMillis();  
  224.                                 int seconds = (int)((endms - startms)/1000);  
  225.                                 int ms = (int)((endms - startms)%1000);  
  226.                                   
  227.                                 Message msg = new Message();  
  228.                                 msg.what = 0;  
  229.                                 // 使用bundle对象来传递数据  
  230.                                 Bundle b = new Bundle();  
  231.                                 if(seconds > 0) {  
  232.                                     b.putInt("seconds", seconds);  
  233.                                 }  
  234.                                 b.putInt("ms", ms);  
  235.                                 msg.setData(b);  
  236.                                 mHandler.sendMessage(msg);  
  237.                             } catch(Exception e) {  
  238.                                 e.printStackTrace();  
  239.                             } finally {  
  240.                                 db.close(); // 关闭数据库  
  241.                             }  
  242.                         }  
  243.                     }.start();  
  244.                 } else {  
  245.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  246.                 }  
  247.             }  
  248.         });  
  249.           
  250.         // execSQL插入  
  251.         Button btnExecSqlInsert = (Button)findViewById(R.id.btnExecSqlInsert);  
  252.         btnExecSqlInsert.setOnClickListener(new OnClickListener() {  
  253.   
  254.             @Override  
  255.             public void onClick(View arg0) {  
  256.                 // TODO Auto-generated method stub  
  257.                 if (mDbHelper != null) {  
  258.                     mDialog = ProgressDialog.show(MainActivity.this, "execSQL插入", "正在插入1000条记录,请稍等……", true);  
  259.                     new Thread() { // 开启线程执行防止阻塞  
  260.                         @Override  
  261.                         public void run() {  
  262.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  263.                             try {  
  264.                                 // 利用事物批量插入数据以提高效率  
  265.                                 long startms = System.currentTimeMillis();  
  266.                                 db.beginTransaction();  
  267.                                 for (int i = 0; i < 1000; i++) {  
  268.                                     db.execSQL("INSERT INTO student(name, birthday, gender) values(?, ?, ?)" ,  
  269.                                             new Object[]{new String("李四" + i), "2012-12-08", i%2});  
  270.                                 }  
  271.                                 db.setTransactionSuccessful();  
  272.                                 db.endTransaction();  
  273.                                 long endms = System.currentTimeMillis();  
  274.                                 int seconds = (int)((endms - startms)/1000);  
  275.                                 int ms = (int)((endms - startms)%1000);  
  276.                                   
  277.                                 Message msg = new Message();  
  278.                                 msg.what = 1;  
  279.                                 // 使用bundle对象来传递数据  
  280.                                 Bundle b = new Bundle();  
  281.                                 if(seconds > 0) {  
  282.                                     b.putInt("seconds", seconds);  
  283.                                 }  
  284.                                 b.putInt("ms", ms);  
  285.                                 msg.setData(b);  
  286.                                 mHandler.sendMessage(msg);  
  287.                             } catch(Exception e) {  
  288.                                 e.printStackTrace();  
  289.                             } finally {  
  290.                                 db.close(); // 关闭数据库  
  291.                             }  
  292.                         }  
  293.                     }.start();  
  294.                 }else {  
  295.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  296.                 }  
  297.             }  
  298.               
  299.         });  
  300.           
  301.         // query查询  
  302.         Button btnQuery = (Button)findViewById(R.id.btnQuery);  
  303.         btnQuery.setOnClickListener(new OnClickListener() {  
  304.               
  305.             @Override  
  306.             public void onClick(View arg0) {  
  307.                 // TODO Auto-generated method stub  
  308.                 if (mDbHelper != null) {  
  309.                     mDialog = ProgressDialog.show(MainActivity.this, "query查询", "正在查询记录,请稍等……", true);  
  310.                     new Thread() { // 开启线程执行防止阻塞  
  311.                         @Override  
  312.                         public void run() {  
  313.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  314.                               
  315.                             try {  
  316.                                 Log.d(TAG, "query Start:" + getDate());  
  317.                                 long startms = System.currentTimeMillis();  
  318.                                 // 查询所有记录  
  319.                                 Cursor c = db.query("student", null, null, null, null, null, new String("id ASC LIMIT 0,1000"));  
  320.                                 Log.d(TAG, "query End:" + getDate());  
  321.                                 long endms = System.currentTimeMillis();  
  322.                                 int seconds = (int)((endms - startms)/1000);  
  323.                                 int ms = (int)((endms - startms)%1000);  
  324.                                 if (c != null) {  
  325.                                     Message msg = new Message();  
  326.                                     msg.what = 10;  
  327.                                     // 使用bundle对象来传递数据  
  328.                                     Bundle b = new Bundle();  
  329.                                     b.putInt("count", c.getCount());  
  330.                                     if(seconds > 0) {  
  331.                                         b.putInt("seconds", seconds);  
  332.                                     }  
  333.                                     b.putInt("ms", ms);  
  334.                                     msg.setData(b);  
  335.                                     mHandler.sendMessage(msg);  
  336.                                     c.close();  
  337.                                 }  
  338.                             } catch(Exception e) {  
  339.                                 e.printStackTrace();  
  340.                             } finally {  
  341.                                 db.close(); // 关闭数据库  
  342.                             }  
  343.                         }  
  344.                     }.start();  
  345.                 }else {  
  346.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  347.                 }  
  348.             }  
  349.               
  350.         });  
  351.           
  352.         // rawQuery查询  
  353.         Button btnRawQuery = (Button)findViewById(R.id.btnRawQuery);  
  354.         btnRawQuery.setOnClickListener(new OnClickListener() {  
  355.   
  356.             @Override  
  357.             public void onClick(View arg0) {  
  358.                 // TODO Auto-generated method stub  
  359.                 if (mDbHelper != null) {  
  360.                     mDialog = ProgressDialog.show(MainActivity.this, "rawQuery查询", "正在查询记录,请稍等……", true);  
  361.                     new Thread() { // 开启线程执行防止阻塞  
  362.                         @Override  
  363.                         public void run() {  
  364.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  365.                               
  366.                             try {  
  367.                                 Log.d(TAG, "rawQuery Start:" + getDate());  
  368.                                 long startms = System.currentTimeMillis();  
  369.                                 // 查询所有记录  
  370.                                 Cursor c = db.rawQuery("SELECT * FROM student ORDER BY id ASC LIMIT 0,1000", null);  
  371.                                 Log.d(TAG, "rawQuery End:" + getDate());  
  372.                                 long endms = System.currentTimeMillis();  
  373.                                 int seconds = (int)((endms - startms)/1000);  
  374.                                 int ms = (int)((endms - startms)%1000);  
  375.                                 if (c != null) {  
  376.                                     Message msg = new Message();  
  377.                                     msg.what = 11;  
  378.                                     // 使用bundle对象来传递数据  
  379.                                     Bundle b = new Bundle();  
  380.                                     b.putInt("count", c.getCount());  
  381.                                     if(seconds > 0) {  
  382.                                         b.putInt("seconds", seconds);  
  383.                                     }  
  384.                                     b.putInt("ms", ms);  
  385.                                     msg.setData(b);  
  386.                                     mHandler.sendMessage(msg);  
  387.                                     c.close();  
  388.                                 }  
  389.                             } catch(Exception e) {  
  390.                                 e.printStackTrace();  
  391.                             } finally {  
  392.                                 db.close(); // 关闭数据库  
  393.                             }  
  394.                         }  
  395.                     }.start();  
  396.                 } else {  
  397.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  398.                 }  
  399.             }  
  400.               
  401.         });  
  402.           
  403.         // update更新  
  404.         Button btnUpdate = (Button)findViewById(R.id.btnUpdate);  
  405.         btnUpdate.setOnClickListener(new OnClickListener() {  
  406.               
  407.             @Override  
  408.             public void onClick(View arg0) {  
  409.                 // TODO Auto-generated method stub  
  410.                 if (mDbHelper != null) {  
  411.                     mDialog = ProgressDialog.show(MainActivity.this, "update更新", "正在更新1000条记录,请稍等……", true);  
  412.                     new Thread() { // 开启线程执行防止阻塞  
  413.                         @Override  
  414.                         public void run() {  
  415.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  416.                             try {  
  417.                                 ContentValues values = new ContentValues();  
  418.                                 long startms = System.currentTimeMillis();  
  419.                                 values.put("name", new String("王五"));  
  420.                                 values.put("birthday", "2012-12-09");  
  421.                                 values.put("gender", 0);  
  422.                                 int count = db.update("student", values, new String("id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)"), null);  
  423.                                 long endms = System.currentTimeMillis();  
  424.                                 int seconds = (int)((endms - startms)/1000);  
  425.                                 int ms = (int)((endms - startms)%1000);  
  426.                                   
  427.                                 Message msg = new Message();  
  428.                                 msg.what = 20;  
  429.                                 // 使用bundle对象来传递数据  
  430.                                 Bundle b = new Bundle();  
  431.                                 b.putInt("count", count);  
  432.                                 if(seconds > 0) {  
  433.                                     b.putInt("seconds", seconds);  
  434.                                 }  
  435.                                 b.putInt("ms", ms);  
  436.                                 msg.setData(b);  
  437.                                 mHandler.sendMessage(msg);  
  438.                             } catch(Exception e) {  
  439.                                 e.printStackTrace();  
  440.                             } finally {  
  441.                                 db.close(); // 关闭数据库  
  442.                             }  
  443.                         }  
  444.                     }.start();  
  445.                 } else {  
  446.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  447.                 }  
  448.             }  
  449.         });  
  450.           
  451.         // execSQL更新  
  452.         Button btnExecSqlUpdate = (Button)findViewById(R.id.btnExecSqlUpdate);  
  453.         btnExecSqlUpdate.setOnClickListener(new OnClickListener() {  
  454.   
  455.             @Override  
  456.             public void onClick(View arg0) {  
  457.                 // TODO Auto-generated method stub  
  458.                 if (mDbHelper != null) {  
  459.                     mDialog = ProgressDialog.show(MainActivity.this, "execSQL更新", "正在更新1000条记录,请稍等……", true);  
  460.                     new Thread() { // 开启线程执行防止阻塞  
  461.                         @Override  
  462.                         public void run() {  
  463.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  464.                             try {  
  465.                                 long startms = System.currentTimeMillis();  
  466.                                 db.execSQL("UPDATE student SET name = ?, birthday = ?, gender = ? WHERE id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)" ,  
  467.                                         new Object[]{new String("马六"), "2012-12-10", 1});  
  468.                                 long endms = System.currentTimeMillis();  
  469.                                 int seconds = (int)((endms - startms)/1000);  
  470.                                 int ms = (int)((endms - startms)%1000);  
  471.                                   
  472.                                 Message msg = new Message();  
  473.                                 msg.what = 21;  
  474.                                 // 使用bundle对象来传递数据  
  475.                                 Bundle b = new Bundle();  
  476.                                 if(seconds > 0) {  
  477.                                     b.putInt("seconds", seconds);  
  478.                                 }  
  479.                                 b.putInt("ms", ms);  
  480.                                 msg.setData(b);  
  481.                                 mHandler.sendMessage(msg);  
  482.                             } catch(Exception e) {  
  483.                                 e.printStackTrace();  
  484.                             } finally {  
  485.                                 db.close(); // 关闭数据库  
  486.                             }  
  487.                         }  
  488.                     }.start();  
  489.                 }else {  
  490.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  491.                 }  
  492.             }  
  493.               
  494.         });  
  495.           
  496.         // delete删除  
  497.         Button btnDelete = (Button)findViewById(R.id.btnDelete);  
  498.         btnDelete.setOnClickListener(new OnClickListener() {  
  499.               
  500.             @Override  
  501.             public void onClick(View arg0) {  
  502.                 // TODO Auto-generated method stub  
  503.                 if (mDbHelper != null) {  
  504.                     mDialog = ProgressDialog.show(MainActivity.this, "delete删除", "正在删除1000条记录,请稍等……", true);  
  505.                     new Thread() { // 开启线程执行防止阻塞  
  506.                         @Override  
  507.                         public void run() {  
  508.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  509.                             try {  
  510.                                 long startms = System.currentTimeMillis();  
  511.                                 int count = db.delete("student", new String("id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)"), null);  
  512.                                 long endms = System.currentTimeMillis();  
  513.                                 int seconds = (int)((endms - startms)/1000);  
  514.                                 int ms = (int)((endms - startms)%1000);  
  515.                                   
  516.                                 Message msg = new Message();  
  517.                                 msg.what = 30;  
  518.                                 // 使用bundle对象来传递数据  
  519.                                 Bundle b = new Bundle();  
  520.                                 b.putInt("count", count);  
  521.                                 if(seconds > 0) {  
  522.                                     b.putInt("seconds", seconds);  
  523.                                 }  
  524.                                 b.putInt("ms", ms);  
  525.                                 msg.setData(b);  
  526.                                 mHandler.sendMessage(msg);  
  527.                             } catch(Exception e) {  
  528.                                 e.printStackTrace();  
  529.                             } finally {  
  530.                                 db.close(); // 关闭数据库  
  531.                             }  
  532.                         }  
  533.                     }.start();  
  534.                 } else {  
  535.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  536.                 }  
  537.             }  
  538.         });  
  539.           
  540.         // execSQL删除  
  541.         Button btnExecSqlDelete = (Button)findViewById(R.id.btnExecSqlDelete);  
  542.         btnExecSqlDelete.setOnClickListener(new OnClickListener() {  
  543.   
  544.             @Override  
  545.             public void onClick(View arg0) {  
  546.                 // TODO Auto-generated method stub  
  547.                 if (mDbHelper != null) {  
  548.                     mDialog = ProgressDialog.show(MainActivity.this, "execSQL删除", "正在删除1000条记录,请稍等……", true);  
  549.                     new Thread() { // 开启线程执行防止阻塞  
  550.                         @Override  
  551.                         public void run() {  
  552.                             SQLiteDatabase db = mDbHelper.getWritableDatabase();  
  553.                             try {  
  554.                                 long startms = System.currentTimeMillis();  
  555.                                 db.execSQL("DELETE FROM student WHERE id IN (SELECT id FROM student ORDER BY id ASC LIMIT 0,1000)");  
  556.                                 long endms = System.currentTimeMillis();  
  557.                                 int seconds = (int)((endms - startms)/1000);  
  558.                                 int ms = (int)((endms - startms)%1000);  
  559.                                   
  560.                                 Message msg = new Message();  
  561.                                 msg.what = 31;  
  562.                                 // 使用bundle对象来传递数据  
  563.                                 Bundle b = new Bundle();  
  564.                                 if(seconds > 0) {  
  565.                                     b.putInt("seconds", seconds);  
  566.                                 }  
  567.                                 b.putInt("ms", ms);  
  568.                                 msg.setData(b);  
  569.                                 mHandler.sendMessage(msg);  
  570.                             } catch(Exception e) {  
  571.                                 e.printStackTrace();  
  572.                             } finally {  
  573.                                 db.close(); // 关闭数据库  
  574.                             }  
  575.                         }  
  576.                     }.start();  
  577.                 }else {  
  578.                     Toast.makeText(MainActivity.this, "请先创建数据库!", Toast.LENGTH_SHORT).show();  
  579.                 }  
  580.             }  
  581.               
  582.         });  
  583.           
  584.         mTvInfo = (TextView)findViewById(R.id.tvInfo);  
  585.     }  
  586.   
  587.     @Override  
  588.     public boolean onCreateOptionsMenu(Menu menu) {  
  589.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  590.         return true;  
  591.     }  
  592.       
  593.     /**   
  594.      * 获取系统时间   
  595.      * @return   
  596.      */    
  597.     public static String getDate(){  
  598.         Calendar ca = Calendar.getInstance();     
  599.         int year = ca.get(Calendar.YEAR);           // 获取年份     
  600.         int month = ca.get(Calendar.MONTH);         // 获取月份      
  601.         int day = ca.get(Calendar.DATE);            // 获取日     
  602.         int minute = ca.get(Calendar.MINUTE);       // 分      
  603.         int hour = ca.get(Calendar.HOUR);           // 小时      
  604.         int second = ca.get(Calendar.SECOND);       // 秒     
  605.         int millisecond = ca.get(Calendar.MILLISECOND); // 毫秒  
  606.        
  607.         String date = year + "/" + (month + 1 )+ "/" + day + " "+ hour + ":" + minute + ":" + second + ":" + millisecond;     
  608.     
  609.         return date;           
  610.     }  
  611. }  

执行效果图:

 

技术分享

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android Sqlite数据库执行插入查询更新删除的操作对比

标签:

原文地址:http://www.cnblogs.com/Free-Thinker/p/4711446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!