1.继承SQLiteOpenHelper:
package com.example.activity_sqlite;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.view.Menu;
/**
* DataBaseHepler作为一个访问SqLite的助手类,提供两个方面的功能
* 1.getReadableDatabase(),getWritableDatabase(),可以获得SQLiteDatabase对象,通过该对象可以对数据库进行操作
* 2.提供了onCreate()和onUpgrade()两个回调函数,允许我们在创建和升级数据库时,进行自己的操作
* @author yzc
*
*/
public class DataBaseHepler extends SQLiteOpenHelper {
private static final int Version=1;
//在SQLiteOpenHelper子类当中必须有该构造函数
/*context:当前activity.this
name:数据库名
factory :null
version:版本号,递增的(1,2,3.。。。。。。)*/
public DataBaseHepler(Context context, String name, CursorFactory factory,
int version) {
//必须通过super调用父类中的构造函数
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public DataBaseHepler(Context context, String name, int version){
this(context, name, null, version);
}
public DataBaseHepler(Context context, String name){
this(context, name,Version );
}
//该函数是在第一次创建数据库的时候执行,实际上是第一次得到SQLiteDatabase对象的时候,才会调用该方法
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("create a DataBase");
//execSQL函数用于执行SQL语句
db.execSQL("create table user(id int,name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("Update a DataBase");
}
}
2.创建表:
DBHelper contact_db;
SQLiteDatabase contact_mDataBase;
private static final String Contact_TABLE = "ContactTable";
public void execSQL(){
contact_db = new DBHelper(LoginActivity.this, Contact_TABLE, null, 1);
contact_mDataBase = contact_db.getWritableDatabase();
contact_mDataBase.execSQL("DROP TABLE IF EXISTS ContactTable");
contact_mDataBase
.execSQL("create table ContactTable(id INTEGER PRIMARY KEY AUTOINCREMENT,"
+ "ContactId varchar(60),ContactName varchar(60),ContactDate varchar(60), ContactPhone varchar(60),ContactCount varchar(60))");
}
3.查询操作:
DBHelper db;
SQLiteDatabase mDataBase;
private static final String Contact_TABLE = "ContactTable";
int count=0;
public ArrayList<ArrayList<String>> getSqlMsg(){
ArrayList<ArrayList<String>> list=new ArrayList<ArrayList<String>>();
try{
Cursor cursor= mDataBase.rawQuery("select * from ContactTable order by ContactDate desc", null);
while(cursor.moveToNext()){
ArrayList<String> arrayList=new ArrayList<String>();
String ContactId=cursor.getString(cursor.getColumnIndex("ContactId"));
String ContactName=cursor.getString(cursor.getColumnIndex("ContactName"));
String ContactDate=cursor.getString(cursor.getColumnIndex("ContactDate"));
String ContactPhone=cursor.getString(cursor.getColumnIndex("ContactPhone"));
String ContactCount=cursor.getString(cursor.getColumnIndex("ContactCount"));
System.out.println("----***"+ContactId+ContactName+ContactDate+ContactPhone+ContactCount);
arrayList.add(ContactId);
arrayList.add(ContactName);
arrayList.add(ContactDate);
arrayList.add(ContactPhone);
arrayList.add(ContactCount+"");
list.add(arrayList);
}
cursor.close();
}catch(Exception e){
Toast.makeText(this, "数据库的表不存在", Toast.LENGTH_LONG);
e.printStackTrace();
}
return list;
}
4.删除操作:
/**
* 删除数据库全部信息
* @param sql_name
* @return
*/
public boolean deleteAllInfos(String sql_name){
int delete= mDataBase.delete(sql_name, null, null);//返回删除了几条数据
System.out.println("执行删除操作!"+delete);
if(delete>=0){
return true;
}else{
return false;
}
}
5.更新操作:
ContentValues cv = new ContentValues();
cv.put("ContactName", SingleString[1]);
System.out.println("---"+SingleString[1]);
cv.put("ContactDate", date);
cv.put("ContactPhone", phone_number);
cv.put("ContactId", ContactAreaNum+SingleString[0]);
mDataBase.update(Contact_TABLE, cv, "ContactId=?", new String[]{list.get(i).get(0)});
6.增加操作:
ContentValues cv = new ContentValues();
cv.put("ContactName", SingleString[1]);
System.out.println("---"+SingleString[1]);
cv.put("ContactDate", date);
cv.put("ContactPhone", phone_number);
cv.put("ContactId", ContactAreaNum+SingleString[0]);
mDataBase.insert(Contact_TABLE, null, cv);