<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SQLite 使用詳解

          共 8702字,需瀏覽 18分鐘

           ·

          2020-11-11 11:03

          和你一起終身學(xué)習(xí),這里是程序員Android

          經(jīng)典好文推薦,通過(guò)閱讀本文,您將收獲以下知識(shí)點(diǎn):


          一、創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)表方法
          二、插入數(shù)據(jù)庫(kù)數(shù)據(jù)方法
          三、刪除數(shù)據(jù)庫(kù)數(shù)據(jù)
          四、查詢數(shù)據(jù)庫(kù)數(shù)據(jù)
          五、修改數(shù)據(jù)庫(kù)數(shù)據(jù)
          六、數(shù)據(jù)庫(kù)使用案例

          Android 提供了對(duì) SQLite 數(shù)據(jù)庫(kù)的完全支持。應(yīng)用中的任何類(不包括應(yīng)用外部的類)均可按名稱訪問(wèn)您所創(chuàng)建的任何數(shù)據(jù)庫(kù)。如果想讓自己創(chuàng)建的數(shù)據(jù)庫(kù)供外部應(yīng)用使用,請(qǐng)使用 ContentProvider對(duì)外提供接口。SQLite輕量級(jí)數(shù)據(jù)庫(kù)使用方法如下:

          一、創(chuàng)建數(shù)據(jù)庫(kù),數(shù)據(jù)表方法

          創(chuàng)建數(shù)據(jù)庫(kù)表方法如下:

          1. 語(yǔ)法

          創(chuàng)建數(shù)據(jù)表語(yǔ)法 如下:

          創(chuàng)建數(shù)據(jù)表語(yǔ)法

          2. 舉例

          舉例 如下:

          舉例

          3.繼承SQLiteDBHelper的創(chuàng)建數(shù)據(jù)庫(kù)、數(shù)據(jù)表

          Android 中創(chuàng)建數(shù)據(jù)庫(kù)、數(shù)據(jù)表 方法如下:

          創(chuàng)建數(shù)據(jù)庫(kù)、數(shù)據(jù)表

          4.刪除數(shù)據(jù)庫(kù)表

          刪除數(shù)據(jù)庫(kù)表方法如下:

          DROP TABLE IF EXITS TABLE_NAME;

          二、 插入數(shù)據(jù)庫(kù)數(shù)據(jù)

          1. 插入數(shù)據(jù)方法一

          • 語(yǔ)法

          INSERT INTO TABLE_NAME [(column1, column2, column3,...columnN)]  
          VALUES (value1, value2, value3,...valueN);
          • 舉例

          INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY)
          VALUES (1, 'Paul', 32, 'California', 20000.00 );

          2. 插入數(shù)據(jù)方法二

          • 語(yǔ)法

          INSERT INTO TABLE_NAME VALUES (value1,value2,value3,...valueN);
          • 舉例

          INSERT INTO COMPANY VALUES (7, 'James', 24, 'Houston', 10000.00 );

          3. Android 封裝insert()插入數(shù)據(jù)方法三

          • 語(yǔ)法

          insert(String table, String nullColumnHack, ContentValues values)
          • 舉例

          private SQLiteDatabase db;
          db.insert(SQLiteDBHelper.TABLE_NAME, null, values);

          四、 刪除數(shù)據(jù)庫(kù)數(shù)據(jù)

          1. 刪除數(shù)據(jù)庫(kù)數(shù)據(jù)方法一

          • 語(yǔ)法

          DELETE FROM table_name 
          WHERE [condition];
          • 舉例

          DELETE FROM COMPANY WHERE ID = 7;

          2.Android封裝刪除數(shù)據(jù)庫(kù)數(shù)據(jù)方法二

          • 語(yǔ)法

          delete(String table, String whereClause, String[] whereArgs)
          • 舉例

          private SQLiteDatabase db;
          db.delete(SQLiteDBHelper.TABLE_NAME, "name=?",
          new String[] { "Jack" });

          四、 查詢數(shù)據(jù)庫(kù)數(shù)據(jù)

          1. 查詢數(shù)據(jù)庫(kù)方法一

          • 語(yǔ)法

          SELECT column1, column2, columnN FROM table_name;

          查詢表中所有數(shù)據(jù)的方法

          SELECT * FROM table_name;
          • 舉例

          //1.查詢指定的列
          SELECT ID, NAME, SALARY FROM COMPANY;
          // 2.查詢表中所有內(nèi)容
          SELECT * FROM COMPANY;

          2. Android 封裝查詢數(shù)據(jù)庫(kù)方法二

          • 語(yǔ)法

          query(String table, String[] columns, String selection,
          String[] selectionArgs, String groupBy, String having,
          String orderBy)
          • 舉例

          private SQLiteDatabase db;
          Cursor cursor = db.query(SQLiteDBHelper.TABLE_NAME, null, null, null,
          null, null, null);

          五、 修改數(shù)據(jù)庫(kù)數(shù)據(jù)

          1. 修改數(shù)據(jù)庫(kù)方法一

          • 語(yǔ)法

          UPDATE table_name
          SET column1 = value1, column2 = value2...., columnN = valueN
          WHERE [condition];
          • 舉例

          UPDATE COMPANY SET ADDRESS = 'Texas' WHERE ID = 6;

          2. Android update 封裝修改數(shù)據(jù)庫(kù)方法二

          • 語(yǔ)法

          update(String table, ContentValues values, String whereClause, String[] whereArgs)
          • 舉例

          db.update(SQLiteDBHelper.TABLE_NAME, values, "nickname=?",
          new String[] { "J" });

          六、數(shù)據(jù)庫(kù)使用案例

          • 實(shí)現(xiàn)效果

          數(shù)據(jù)庫(kù)增刪改查

          1. 創(chuàng)建數(shù)據(jù)庫(kù)

          創(chuàng)建數(shù)據(jù)庫(kù) 方法如下:

          public class SQLiteDBHelper extends SQLiteOpenHelper {
          public static String DB_NAME = "person.db";
          // version 必須大于1
          public static int DB_VERSION = 1;
          public static String TABLE_NAME = "person";
          public static String _ID = "_id";
          public static String NAME = "name";
          public static String NICK_NAME = "nickname";

          public SQLiteDBHelper(Context context, String name, CursorFactory factory,
          int version) {
          super(context, name, factory, version);
          // TODO Auto-generated constructor stub
          }

          public SQLiteDBHelper(Context context) {
          super(context, DB_NAME, null, DB_VERSION);
          }

          @Override
          public void onCreate(SQLiteDatabase db) {

          // SQLite 數(shù)據(jù)庫(kù)中,字段一般不區(qū)分類型,但是主鍵除外,主鍵必須是整型
          String sql = "CREATE TABLE " + TABLE_NAME + "(" + _ID
          + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL" + "," + NAME
          + " CHAR(10)," + NICK_NAME + " CHAR(10))";
          db.execSQL(sql);
          }

          // 數(shù)據(jù)庫(kù)升級(jí)的處理方法,
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

          if (newVersion > oldVersion) {
          // 刪除老的數(shù)據(jù)表,創(chuàng)建新表
          String dropsql = "DROP TABLE IF EXITS " + TABLE_NAME;
          db.execSQL(dropsql);
          onCreate(db);
          }
          }

          }

          2. 數(shù)據(jù)庫(kù)增加數(shù)據(jù)方法實(shí)現(xiàn)

          數(shù)據(jù)庫(kù)增加數(shù)據(jù)方法實(shí)現(xiàn)代碼如下:

              //
          public void InsertSQL(View view) {

          InstertDB();
          QueryDB();
          }

          /**
          * 插入數(shù)據(jù)處理方法
          */

          private void InstertDB() {
          ContentValues values = new ContentValues();
          values.put("name", "Jack");
          values.put("nickname", "J");

          // 返回值:最近插入的那一行的行號(hào)
          long result = db.insert(SQLiteDBHelper.TABLE_NAME, null, values);
          if (result > 0) {
          Toast.makeText(getApplicationContext(), "添加成功", Toast.LENGTH_SHORT)
          .show();
          } else {
          Toast.makeText(getApplicationContext(), "添加失敗", Toast.LENGTH_SHORT)
          .show();
          }
          }

          3. 數(shù)據(jù)庫(kù)刪除內(nèi)容方法實(shí)現(xiàn)

          數(shù)據(jù)庫(kù)刪除內(nèi)容方法實(shí)現(xiàn)代碼如下:

              /**
          * 刪除數(shù)據(jù)
          */

          private void DeleteDb() {
          int result = db.delete(SQLiteDBHelper.TABLE_NAME, "name=?",
          new String[] { "Jack" });
          if (result > 0) {
          Toast.makeText(getApplicationContext(), "刪除成功", Toast.LENGTH_SHORT)
          .show();
          } else {
          Toast.makeText(getApplicationContext(), "無(wú)Jack", Toast.LENGTH_SHORT)
          .show();
          }
          }

          4. 數(shù)據(jù)庫(kù)修改內(nèi)容方法實(shí)現(xiàn)

          數(shù)據(jù)庫(kù)修改內(nèi)容方法實(shí)現(xiàn)代碼如下:

              /**
          * 修改數(shù)據(jù)
          */

          private void UpdateDb() {
          // update person set name="Kitty" where nickname="J"
          ContentValues values = new ContentValues();
          values.put("name", "Lucy");

          int result = db.update(SQLiteDBHelper.TABLE_NAME, values, "nickname=?",
          new String[] { "J" });

          if (result > 0) {
          QueryDB();
          }
          }

          5. 數(shù)據(jù)庫(kù)查詢方法實(shí)現(xiàn)

          數(shù)據(jù)庫(kù)查詢方法實(shí)現(xiàn)代碼如下:

              /**
          * 查詢數(shù)據(jù)處理方法
          */

          private void QueryDB() {
          // Select * from person where name= ?
          // db.rawQuery(s)
          // cursor 游標(biāo)--》 結(jié)果集合
          // 當(dāng)使用SimpleCusorAdapter 時(shí)候,cursor 這個(gè)記過(guò)里面 必須包含“_id”,這個(gè)字段
          // Cursor cursor = db.query(SQLiteDBHelper.TABLE_NAME, null, "name=?",
          // new String[] { "Jack" }, null, null, null);
          Cursor cursor = db.query(SQLiteDBHelper.TABLE_NAME, null, null, null,
          null, null, null);

          while (cursor.moveToNext()) {

          String name = cursor.getString(cursor.getColumnIndex("name"));
          String nickname = cursor.getString(cursor
          .getColumnIndex("nickname"));
          }
          // 將一個(gè)新的cusor跟原有的custor 交換
          adapter.swapCursor(cursor);
          adapter.notifyDataSetChanged();
          }

          6. 布局如下


          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical" >

          <Button
          android:id="@+id/insertdata"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="InsertSQL"
          android:text="增加數(shù)據(jù)" />

          <Button
          android:id="@+id/deletedata"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="DeleteSQL"
          android:text="刪除數(shù)據(jù)" />

          <Button
          android:id="@+id/quarydata"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="QuarySQL"
          android:text="查詢數(shù)據(jù)" />

          <Button
          android:id="@+id/modifydata"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:onClick="ModifySQL"
          android:text="修改數(shù)據(jù)" />

          <ListView
          android:id="@+id/lv"
          android:layout_width="match_parent"
          android:layout_height="match_parent" />

          LinearLayout>

          7. ListView item布局如下


          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:gravity="center_vertical"
          android:orientation="horizontal"
          android:padding="10dp" >


          <TextView
          android:id="@+id/name"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="name" />


          <TextView
          android:id="@+id/nickname"
          android:layout_width="0dp"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:text="nickname" />


          LinearLayout>

          至此,本篇已結(jié)束。轉(zhuǎn)載網(wǎng)絡(luò)的文章,小編覺(jué)得很優(yōu)秀,歡迎點(diǎn)擊閱讀原文,支持原創(chuàng)作者,如有侵權(quán),懇請(qǐng)聯(lián)系小編刪除。同時(shí)感謝您的閱讀,期待您的關(guān)注。

          瀏覽 54
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  殴美肏屄视频免费看 | 国产自研AV在线播放 | 精品人人妻人人澡人人爽牛牛 | sm免费网站 Xx视频 | 免费在线观看黄色视频链接 |