Android基础之sqlite 数据库简单操作

尽管很简单,但是也存下来,以后直接粘过去就能用了。

public class DBHelper extends SQLiteOpenHelper {  
    private static final String DB_NAME = "lintest.db";  
    private static final String TBL_NAME = "lin";  
    private static final String CREATE_TBL = " create table "  
            + " CollTbl(_id integer primary key autoincrement,name text,url text, desc text) ";  
      
    private SQLiteDatabase db;  
    DBHelper(Context c) {  
        super(c, DB_NAME, null, 2);  
    }  
    @Override  
    public void onCreate(SQLiteDatabase db) {  
        this.db = db;  
        db.execSQL(CREATE_TBL);  
    }  
    public void insert(ContentValues values) {  
        SQLiteDatabase db = getWritableDatabase();  
        db.insert(TBL_NAME, null, values);  
        db.close();  
    }  
    public Cursor query() {  
        SQLiteDatabase db = getWritableDatabase();  
        Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);  
        return c;  
    }  
    public void del(int id) {  
        if (db == null)  
            db = getWritableDatabase();  
        db.delete(TBL_NAME, "_id=?", new String[] { String.valueOf(id) });  
    }  
    public void close() {  
        if (db != null)  
            db.close();  
    }  
    @Override  
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
    }  

 

 

     存入数据库内容

                      String name =  "lin"

  •                 String url = "http://www.cnblogs.com/CaptainLin"
  •                 String desc = "test"
  •                 ContentValues values = new ContentValues();  
  •                 values.put("name", name);  
  •                 values.put("url", url);  
  •                 values.put("desc", desc);  
  •                 DBHelper helper = new DBHelper(getApplicationContext());  
  •                 helper.insert(values);

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