标签:
package com.example.wxj2048; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class Dbdao extends SQLiteOpenHelper{ private static final String DBNAME = "mldn.db"; private static final int DBVERSION = 1; private static final String TABNAME = "game2048"; public Dbdao(Context context){ super(context, DBNAME, null, DBVERSION); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub String sql =" CREATE TABLE "+ TABNAME+" (" + " id INTEGER ," + " score INTEGER "+ ") "; db.execSQL(sql); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub String sql = " DROP TABLE IF EXISTS " + TABNAME; db.execSQL(sql); this.onCreate(db); } public void insert(Integer score){ SQLiteDatabase db = getWritableDatabase(); ContentValues values = new ContentValues(); values.put("id", 1); values.put("score", score); db.insert(TABNAME, null, values); db.close(); } public void delete(){ SQLiteDatabase db = getWritableDatabase(); String[] data = new String[1]; data[0] = "1"; db.delete(TABNAME,"id = ?",data); db.close(); } public Integer query(){ SQLiteDatabase db = getWritableDatabase(); Cursor c = db.query(TABNAME, null, null, null, null, null, null); c.moveToFirst(); int index = c.getColumnIndex("score"); String result = c.getString(index); db.close(); return Integer.valueOf(result); } }第二步;就可以创建 Dbdao 对象,然后使用数据库
标签:
原文地址:http://blog.csdn.net/cqs_experiment/article/details/45060219