码迷,mamicode.com
首页 > 数据库 > 详细

SQLiteDatabase的使用

时间:2014-11-28 14:24:06      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   io   ar   color   os   使用   sp   

新建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()

 

SQLiteDatabase的使用

标签:android   style   blog   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/hixin/p/4128205.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!