使用SQLiteDatabase操作SQLite数据库存储数据

前端之家收集整理的这篇文章主要介绍了使用SQLiteDatabase操作SQLite数据库存储数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
MainActivity.java
  1. package practise.lxm.hello;
  2.  
  3. import android.app.Activity;
  4. import android.database.Cursor;
  5. import android.database.sqlite.sqliteDatabase;
  6. import android.database.sqlite.sqliteException;
  7. import android.os.Environment;
  8. import android.os.Bundle;
  9. import android.view.View;
  10. import android.widget.Button;
  11. import android.widget.CursorAdapter;
  12. import android.widget.EditText;
  13. import android.widget.ListView;
  14. import android.widget.SimpleCursorAdapter;
  15. import java.io.File;
  16. import java.io.IOException;
  17.  
  18. public class MainActivity extends Activity {
  19.  
  20. sqliteDatabase db;
  21. EditText edTxtTitle;
  22. EditText edTxtContent;
  23. ListView lvNews;
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28.  
  29. edTxtTitle = (EditText)findViewById(R.id.edTxt_title);
  30. edTxtContent = (EditText)findViewById(R.id.edTxt_content);
  31. lvNews = (ListView)findViewById(R.id.lv_test);
  32. Button btn = (Button)findViewById(R.id.btn_commit);
  33.  
  34. //打开或创建数据库
  35. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
  36. try {
  37. File file = new File(Environment.getExternalStorageDirectory().getCanonicalPath() + "//test.db3");
  38. db = sqliteDatabase.openOrCreateDatabase(file,null);
  39.  
  40. } catch (IOException e) {
  41. e.printStackTrace();
  42. }
  43. }
  44.  
  45. btn.setOnClickListener(new View.OnClickListener() {
  46. @Override
  47. public void onClick(View v) {
  48. try {
  49. insert(db,edTxtTitle.getText().toString(),edTxtContent.getText().toString()); //插入
  50. }catch (sqliteException sql){
  51. //失败创建表
  52. String strsql = "create table news(_id integer primary key autoincrement,title nvarchar(50) not null,content nvarchar(100) not null)";
  53. db.execsql(strsql);
  54. //重新插入
  55. insert(db,edTxtContent.getText().toString());
  56. }
  57. //刷新列表
  58. String strsql = "select * from news";
  59. Cursor cursor= db.rawQuery(strsql,null); //查询
  60. inflateList(cursor);
  61. }
  62. });
  63. }
  64.  
  65.  
  66. //插入信息
  67. private void insert(sqliteDatabase db,String title,String content){
  68. String strInsert = "insert into news(title,content) values(?,?)";
  69. db.execsql(strInsert,new String[]{title,content});
  70. }
  71.  
  72. //显示数据到列表
  73. private void inflateList(Cursor cursor){
  74. //表中必须含有名为"_id"的列,该列数据类型等无所谓
  75. SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,R.layout.lv_file_item,cursor,new String[]{"title","content"},new int[]{R.id.tv_title,R.id.tv_content},CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
  76. lvNews.setAdapter(simpleCursorAdapter);
  77. }
  78.  
  79. protected void onDestroy() {
  80. if(db != null) { //关闭数据库连接
  81. db.close();
  82. }
  83. super.onDestroy();
  84. }
  85.  
  86. }

lv_file_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical" android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <TextView
  6. android:layout_width="match_parent"
  7. android:layout_height="wrap_content"
  8. android:textStyle="bold"
  9. android:textSize="20sp"
  10. android:id="@+id/tv_title"/>
  11. <TextView
  12. android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:textSize="15sp"
  15. android:id="@+id/tv_content"/>
  16. </LinearLayout>

activity_main.xml

  1. <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:stretchColumns="1"
  6. android:padding="5pt">
  7. <TableRow android:padding="5dp">
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="标题"/>
  12. <EditText android:layout_width="match_parent"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/edTxt_title"/>
  15. </TableRow>
  16. <TableRow android:padding="5dp">
  17. <TextView
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:text="内容"/>
  21. <EditText android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:id="@+id/edTxt_content"/>
  24. </TableRow>
  25. <Button
  26. android:text="提交"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. android:id="@+id/btn_commit"/>
  30. <ListView
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:id="@+id/lv_test"></ListView>
  34. </TableLayout>

猜你在找的Sqlite相关文章