Android SQLite数据库的添 删 查 改

首先要创建一个类,继承自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>

  接下来就是主窗体的代码部分了

Android SQLite数据库的添 删 查 改

  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();
   }

你可能感兴趣的:(android,sqlite)