數據庫SQList 添加 查詢 修改 刪除


SQListActivity

  1 package com.example.administrator.myapplication.database.Activity;
  2 
  3 import android.content.ContentValues;
  4 import android.database.Cursor;
  5 import android.database.sqlite.SQLiteDatabase;
  6 import android.os.Bundle;
  7 import android.support.v7.app.AppCompatActivity;
  8 import android.view.View;
  9 import android.widget.Button;
 10 import android.widget.EditText;
 11 import android.widget.Toast;
 12 
 13 import com.example.administrator.myapplication.R;
 14 import com.example.administrator.myapplication.database.MySQLiteOpenHelper;
 15 import com.example.administrator.myapplication.util.Common;
 16 
 17 public class SQLiteActivity extends AppCompatActivity {
 18     Button insertBtn;
 19     Button queryBtn;
 20     Button deleteBtn;
 21     Button updateBtn;
 22     EditText titleET;
 23     EditText deleteET;
 24     EditText updateET;
 25     String TitleReceiver;
 26     String deleteReceiver;
 27     String updateReceiver;
 28 
 29     @Override
 30     protected void onCreate(Bundle savedInstanceState) {
 31         super.onCreate(savedInstanceState);
 32         setContentView(R.layout.activity_sqlite);
 33         titleET = (EditText) findViewById(R.id.titleText);
 34         deleteET = (EditText) findViewById(R.id.deleteText);
 35         updateET = (EditText) findViewById(R.id.updateText);
 36         insertBtn = (Button) findViewById(R.id.insertData);
 37         insertBtn.setOnClickListener(new View.OnClickListener() {
 38             @Override
 39             public void onClick(View v) {
 40                 insertData();
 41             }
 42         });
 43         queryBtn = (Button) findViewById(R.id.queryData);
 44         queryBtn.setOnClickListener(new View.OnClickListener() {
 45             @Override
 46             public void onClick(View v) {
 47                 queryData();
 48             }
 49         });
 50         deleteBtn = (Button) findViewById(R.id.deleteData);
 51         deleteBtn.setOnClickListener(new View.OnClickListener() {
 52             @Override
 53             public void onClick(View v) {
 54                 deleteData();
 55             }
 56         });
 57         updateBtn = (Button) findViewById(R.id.updateData);
 58         updateBtn.setOnClickListener(new View.OnClickListener() {
 59             @Override
 60             public void onClick(View v) {
 61                 updateData();
 62             }
 63         });
 64     }
 65 
 66     //插入數據
 67     public void insertData(){
 68         TitleReceiver = titleET.getText().toString();
 69         //調用幫助類的構造函數,創建數據庫
 70         MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
 71         //調用幫助類的onCreate方法,創建表
 72         SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
 73         //將保存的數據放入ContentValues中
 74         ContentValues contentValues = new ContentValues();
 75         contentValues.put(Common.NewsTable.TBL_TITLE,TitleReceiver);
 76         contentValues.put(Common.NewsTable.TBL_CONTENT,"內容");
 77         contentValues.put(Common.NewsTable.TBL_SRC,"圖片");
 78         contentValues.put(Common.NewsTable.TBL_DATE,"日期");
 79         //調用insert方法,保存數據
 80         sqLiteDatabase.insert(Common.NewsTable.TBL_NAME,null,contentValues);
 81         //!!!數據庫操作完成后,一定要關閉數據庫
 82         sqLiteDatabase.close();
 83         Toast.makeText(getApplication(),"保存數據成功!",Toast.LENGTH_SHORT).show();
 84     }
 85     //查詢數據
 86     public void queryData(){
 87         MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
 88         SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getReadableDatabase();
 89         //調用SQLiteDatabase的Query方法,返回數據庫查詢的結果集
 90         Cursor cursor = sqLiteDatabase.query(
 91                 Common.NewsTable.TBL_NAME,
 92                 new String[]{Common.NewsTable.TBL_TITLE,Common.NewsTable.TBL_CONTENT,Common.NewsTable.TBL_SRC,Common.NewsTable.TBL_DATE},
 93                 null,null,null,null,null
 94         );
 95         //!!查詢對應數據的時候,一定要現獲取列的索引值
 96         while (cursor.moveToNext()){
 97             int titleIndex = cursor.getColumnIndex(Common.NewsTable.TBL_TITLE);
 98             String title = cursor.getString(titleIndex);
 99             Toast.makeText(getApplication(),title,Toast.LENGTH_SHORT).show();
100         }
101         //數據查詢完畢以后,1:關系結果集 2.關閉SQLiteDataBase
102         cursor.close();
103         sqLiteDatabase.close();
104         Toast.makeText(getApplication(),"查詢完畢!",Toast.LENGTH_SHORT).show();
105     }
106     //刪除數據
107     private void deleteData() {
108         deleteReceiver = deleteET.getText().toString();
109         MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
110         SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
111         sqLiteDatabase.delete(
112                 Common.NewsTable.TBL_NAME,
113                 // ?:占位符 " = ? "相當於 " = 標題 ", null
114                 Common.NewsTable.TBL_TITLE+ " = ?",
115                 new String[]{deleteReceiver}
116         );
117         sqLiteDatabase.close();
118         Toast.makeText(getApplication(),"刪除成功!",Toast.LENGTH_SHORT).show();
119     }
120     //修改數據
121     private void updateData() {
122         updateReceiver = updateET.getText().toString();
123         MySQLiteOpenHelper mySQLiteOpenHelper = new MySQLiteOpenHelper(this);
124         SQLiteDatabase sqLiteDatabase = mySQLiteOpenHelper.getWritableDatabase();
125         ContentValues contentValues = new ContentValues();
126         contentValues.put(Common.NewsTable.TBL_TITLE,"這是修改后的數據");
127         sqLiteDatabase.update(
128                 Common.NewsTable.TBL_NAME,
129                 contentValues,
130                 Common.NewsTable.TBL_TITLE + " = ?",
131                 new String[]{updateReceiver}
132         );
133         sqLiteDatabase.close();
134         Toast.makeText(getApplication(),"修改成功!",Toast.LENGTH_SHORT).show();
135     }
136 }
MySQListOPENHelper   創建數據庫 創建表
 1 package com.example.administrator.myapplication.database;
 2 
 3 import android.content.Context;
 4 import android.database.sqlite.SQLiteDatabase;
 5 import android.database.sqlite.SQLiteOpenHelper;
 6 
 7 import com.example.administrator.myapplication.util.Common;
 8 
 9 /**
10  * Created by Administrator on 2016-9-19.
11  */
12 
13 //幫助類繼承SQLiteOpenHelper,方法:構造函數,創建數據庫,創建表,更新表
14 public class MySQLiteOpenHelper extends SQLiteOpenHelper {
15     private static final String DB_Name = "new.db"; //數據庫后綴為db,常量用大寫
16     private static final int VERSION = 1; //數據庫版本號
17     //上下文,數據庫名字,factory默認null,版本號
18     /*public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
19         super(context, DB_Name, factory, VERSION);
20     }*/
21     //實現了數據庫的創建,factory默認null,版本號固定
22     public MySQLiteOpenHelper(Context context) {
23         super(context, DB_Name, null, VERSION);
24     }
25 
26     //在onCreate方法中創建表,只執行一次
27     @Override
28     public void onCreate(SQLiteDatabase db) {
29         String sql = Common.NewsTable.getCreateTableSQL();
30         db.execSQL(sql);
31     }
32     //onUpgrade更新表
33     @Override
34     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
35         //先刪除原表,再調用onCreate創建新表
36         db.execSQL("drop table "+ Common.NewsTable.TBL_NAME);
37         onCreate(db);
38     }
39 }

Common 公共類

 1 package com.example.administrator.myapplication.util;
 2 
 3 /**
 4  * Created by Administrator on 2016-9-19.
 5  */
 6 public class Common {
 7     public static class NewsTable{
 8         public static final String TBL_NAME ="News";
 9         public static final String TBL_TITLE = "NewsTitle";
10         public static final String TBL_CONTENT = "NewsContent";
11         public static final String TBL_SRC = "NewsImg";
12         public static final String TBL_DATE = "NewsDate";
13 
14         public static String getCreateTableSQL(){
15             String sql = "create table if not exists "
16                         + TBL_NAME
17                         + "("
18                         + " _id integer primary key autoincrement,"
19                         + TBL_TITLE + " text,"
20                         + TBL_CONTENT + " text,"
21                         + TBL_SRC + " text,"
22                         + TBL_DATE + " varchar(50)"
23                         + ")";
24             return sql;
25         }
26     }
27 }

activity_sqlite

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".database.Activity.SQLiteActivity">
 8 
 9     <EditText
10         android:id="@+id/titleText"
11         android:layout_width="match_parent"
12         android:layout_height="wrap_content"
13         android:hint="請輸入標題" />
14 
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal">
19 
20         <TextView
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="請輸入要刪除的數據" />
24 
25         <EditText
26             android:id="@+id/deleteText"
27             android:layout_width="match_parent"
28             android:layout_height="wrap_content" />
29     </LinearLayout>
30 
31     <LinearLayout
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:orientation="horizontal">
35 
36         <TextView
37             android:layout_width="wrap_content"
38             android:layout_height="wrap_content"
39             android:text="請輸入要修改的數據" />
40 
41         <EditText
42             android:id="@+id/updateText"
43             android:layout_width="match_parent"
44             android:layout_height="wrap_content" />
45     </LinearLayout>
46 
47     <Button
48         android:id="@+id/insertData"
49         android:layout_width="match_parent"
50         android:layout_height="wrap_content"
51         android:text="添加數據" />
52 
53     <Button
54         android:id="@+id/queryData"
55         android:layout_width="match_parent"
56         android:layout_height="wrap_content"
57         android:text="查找數據  " />
58 
59     <Button
60         android:id="@+id/updateData"
61         android:layout_width="match_parent"
62         android:layout_height="wrap_content"
63         android:text="修改數據" />
64 
65     <Button
66         android:id="@+id/deleteData"
67         android:layout_width="match_parent"
68         android:layout_height="wrap_content"
69         android:text="刪除數據" />
70 </LinearLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM