SQLiteDatabase的使用

新建DBHeler.JAVA

 1 package com.hixin.db;
 2 
 3 import com.hixin.contact.User;
 4 
 5 import android.content.ContentValues;
 6 import android.content.Context;
 7 import android.database.sqlite.SQLiteDatabase;
 8 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 9 import android.database.sqlite.SQLiteOpenHelper;
10 
11 public class DBHelper extends SQLiteOpenHelper{
12     public final static  String  DB_NAME = "contact";
13     public final static  int VERSION = 1;
14     private static DBHelper instance = null;
15     private SQLiteDatabase db;
16     
17     //单例模式
18     private DBHelper(Context context) {
19         super(context,DB_NAME,null,VERSION);
20     }
21     
22     public static DBHelper getInstance(Context context) {
23         if(instance == null) {
24             instance = new DBHelper(context);
25         }
26         return instance;
27     }
28     private void openDatabase() {
29         if(db == null) {
30             db = this.getReadableDatabase();
31         }
32     }
33     
34     @Override
35     public void onCreate(SQLiteDatabase db) {
36         // TODO Auto-generated method stub
37          StringBuffer tableCreate = new StringBuffer();
38          tableCreate.append("create table user (_id integer primary key autoincrement,")
39          .append("name text,")
40          .append("mobilephone text,")
41          .append("familyphone text,")
42          .append("officephone text,")
43          .append("position text,")
44          .append("company text,")
45          .append("address text,")
46          .append("email text,")
47          .append("othercontact text,")
48          .append("zipcode text,")
49          .append("remark text,")
50          .append("imagedid int)");
51          
52          db.execSQL(tableCreate.toString());
53     }
54 
55     @Override
56     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
57         // TODO Auto-generated method stub
58         String sql = "drop table if exists user";
59         db.execSQL(sql);
60         onCreate(db);
61     }
62     
63     public void save(User user) {
64         openDatabase();
65         ContentValues value = new ContentValues();
66         value.put("name", user.username);
67         value.put("mobilephone", user.mobilePhone);
68         value.put("familyphone", user.familyPhone);
69         value.put("officephone", user.officePhone);
70         value.put("position", user.position);
71         value.put("address", user.address);
72         value.put("email", user.email);
73         value.put("othercontact", user.otherContact);
74         value.put("zipcode", user.zipCode);
75         value.put("remark", user.remark);
76         value.put("imagedid", user.imageId);
77         
78         db.insert("user", null, value);
79     }
80 
81 }


主函数中调用 
    //save user to database
    DBHelper.getInstance(MainActivity.this).save(user);

 

save()调用openDatabase(),如果数据库不存在,则自动调用数据库的onCreate()

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。