Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android SQLite數據庫的添 刪 查 改

Android SQLite數據庫的添 刪 查 改

編輯:關於android開發

  首先要創建一個類,繼承自SQLiteOpenHelper,用來打開數據庫

  package com.SQLiteTest2;

  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteDatabase.CursorFactory;
  import android.database.sqlite.SQLiteOpenHelper;

  public class MyOpenHelper extends SQLiteOpenHelper {
      public static final String DB_NAME = "student";
      public static final String TABLE_NAME = "student_information";
      public static final String ID = "_id";
      public static final String NUMBER = "number";
      public static final String NAME = "name";

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

      @Override
      public void onCreate(SQLiteDatabase db) {
          // TODO Auto-generated method stub
          db.execSQL("create table if not exists "+TABLE_NAME+" ("   
                  + ID + " integer primary key,"
                  + NUMBER + " varchar,"   
                  + NAME + " varchar)");
         

      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // TODO Auto-generated method stub

      }

  }

  在onCreate()函數中,我創建一個表,用來存儲學生的學號和姓名信息
  然後從UI上的TextEdit控件上獲取數據

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
  <TextView 
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/enter"
      />  
      <LinearLayout
      android:orientation="horizontal"
      android:id="@+id/LinearLayout01"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/number"
      />
      <EditText
      android:id="@+id/number"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"></EditText>
      </LinearLayout>
     
      <LinearLayout
      android:orientation="horizontal"
      android:id="@+id/LinearLayout02"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content">
      <TextView 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/name"
      />
      <EditText
      android:id="@+id/name"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"></EditText>
      </LinearLayout>
  <Button 
      android:id="@+id/submit"
      android:text="@string/submit"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"></Button>
  <TextView 
      android:id="@+id/IdLongitude"
      android:textSize="16px"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      />
  <TextView
      android:id="@+id/IdLatitude"
      android:textSize="16px"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      />
  </LinearLayout>

  接下來就是主窗體的代碼部分了

  import static com.SQLiteTest2.MyOpenHelper.*;
  public class SQLiteTest2 extends Activity {
  MyOpenHelper myHelper;
      EditText [] textArray;
      Button btn_save;
      int [] textIds = {
              R.id.number,
              R.id.name
      };
  View.OnClickListener myListener = new View.OnClickListener() {
          @Override
          public void onClick(View v) {            //保存按鈕按下觸發該事件
              String [] strArray = new String[textArray.length];
              for(int i = 0; i < strArray.length; i++){
                  strArray[i] = textArray[i].getText().toString().trim();    //獲得用戶輸入的

  信息數組
               
              }
              insert(strArray);       
               
              }
       
      };
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          textArray = new EditText[textIds.length];
          for(int i=0;i<textIds.length;i++){
              textArray[i] = (EditText)findViewById(textIds[i]);
          }
          myHelper = new MyOpenHelper(this, MyOpenHelper.DB_NAME, null, 1);
          btn_save = (Button) findViewById(R.id.submit);
          btn_save.setOnClickListener(myListener);
       
      }
     public void insert(String [] strArray)
      {
          SQLiteDatabase db = myHelper.getWritableDatabase();        //獲得數據庫對象
          ContentValues values = new ContentValues();
          values.put(NUMBER, strArray[0]);
          values.put(NAME, strArray[1]);
          long count = db.insert(TABLE_NAME, ID, values);            //插入數據
          db.close();
          if(count == -1){
              Toast.makeText(this, "插入失敗!", Toast.LENGTH_SHORT).show();
          }
          else{
              Toast.makeText(this, "插入成功!", Toast.LENGTH_SHORT).show();
          }
      }
  }

  刪除記錄的函數

  public void deleteContact(int id){
          SQLiteDatabase db = myHelper.getWritableDatabase();        //獲得數據庫對象
          db.delete(TABLE_NAME, ID+"=?", new String[]{id+""});
          db.close();
      }

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved