android 数据库基本操作

package com.example.testandroid;


import java.util.Vector;


import android.Manifest.permission;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DBTest 
{
public static final int VERSION = 2;
///
private final static String DB_NAME = "samdb";
private final static String TABLE_NAME = "person";
private final static String TABLE_COLUNM_ID = "id";
private final static String TABLE_COLUNM_NAME = "name";
private final static String TABLE_COLUNM_AGE = "age";
private final static String TABLE_COLUNM_ADDRESS = "address";

private Context context = null;
private SamSQLHelper samSQLHelper = null;


public DBTest(Context context)
{
this.context = context;
samSQLHelper = new SamSQLHelper(context,VERSION);

}

public synchronized int insertNew(Person person)
{
int id = -1;
SQLiteDatabase sqLiteDatabase = samSQLHelper.getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(TABLE_COLUNM_NAME, person.name);
contentValues.put(TABLE_COLUNM_AGE, person.age);
contentValues.put(TABLE_COLUNM_ADDRESS, person.address);
id = (int)sqLiteDatabase.insert(TABLE_NAME, TABLE_COLUNM_NAME, contentValues);
sqLiteDatabase.close();
} catch (Exception e) {
try {
sqLiteDatabase.close();
} catch (Exception e2) {
}
}
System.out.println("insert id = "+id);

return id;
}

public boolean deleteOld(int id)
{
SQLiteDatabase sqLiteDatabase = samSQLHelper.getWritableDatabase();

try {
String deleteSqlString = String.format("delete from %s where %s = %d", TABLE_NAME,TABLE_COLUNM_ID,id);
System.out.println(deleteSqlString);
sqLiteDatabase.execSQL(deleteSqlString);
sqLiteDatabase.close();
} catch (Exception e) {
e.printStackTrace();
}
return false;
}

public Vector<Person> getAll()
{
Vector<Person> persons = new Vector<Person>();
try {
SQLiteDatabase sqLiteDatabase = samSQLHelper.getReadableDatabase();
String getallsqlString = String.format("select * from %s order by %s desc", TABLE_NAME,TABLE_COLUNM_AGE);
System.out.println(getallsqlString);
Cursor cursor =  sqLiteDatabase.rawQuery(getallsqlString, null);
while(cursor.moveToNext())
{
Person person = new Person();
person.id = cursor.getInt(cursor.getColumnIndex(TABLE_COLUNM_ID));
person.age = cursor.getInt(cursor.getColumnIndex(TABLE_COLUNM_AGE));
person.name = cursor.getString(cursor.getColumnIndex(TABLE_COLUNM_NAME));
person.address = cursor.getString(cursor.getColumnIndex(TABLE_COLUNM_ADDRESS));
persons.add(person);
}
cursor.close();
sqLiteDatabase.close();
} catch (Exception e) {
e.printStackTrace();
}

return persons;
}

public synchronized void updatePerson(Person person)
{
SQLiteDatabase sqLiteDatabase = samSQLHelper.getWritableDatabase();
try {
ContentValues contentValues = new ContentValues();
contentValues.put(TABLE_COLUNM_NAME, person.name);
contentValues.put(TABLE_COLUNM_AGE, person.age);
contentValues.put(TABLE_COLUNM_ADDRESS, person.address);
sqLiteDatabase.update(TABLE_NAME, contentValues, "id=?", new String[]{person.id+""});
sqLiteDatabase.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public synchronized int getCount()
{
int count = 0;
SQLiteDatabase sqLiteDatabase = samSQLHelper.getWritableDatabase();
System.out.println("getVersion = "+sqLiteDatabase.getVersion());
try {
String queryString = String.format("select * from %s", TABLE_NAME);
System.out.println(queryString);
Cursor cursor = sqLiteDatabase.rawQuery(queryString, null);
count = cursor.getCount();
cursor.close();
sqLiteDatabase.close();
} catch (Exception e) {
e.printStackTrace();
}
return count;
}





class SamSQLHelper extends SQLiteOpenHelper
{
public void onCreate(SQLiteDatabase db) 
{
String sql = "create table if not exists "
+ TABLE_NAME + " ("
+ TABLE_COLUNM_ID + " integer primary key,"
+ TABLE_COLUNM_AGE + " integer,"
+ TABLE_COLUNM_NAME + " varchar);";
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

String altertablesql = String.format("alter table %s add %s varchar;", TABLE_NAME,TABLE_COLUNM_ADDRESS);
System.out.println(altertablesql);
db.execSQL(altertablesql);


}

public SamSQLHelper(Context context,int version) {
super(context, DB_NAME, null, version);
}
}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值