1.定义一些数据库的字段名,数据库名,表名,字段名
<span style="font-size:14px;">/**
* 数据库字段类
*
* @author User
*/
public class DataBaseUtils {
public static final String DATABASE_NAME = "ks_launcher.db";
public static final String TABLE_NAME = "user";
public static final int VERSON = 1;
public static final String USER_ID = "user_id";
public static final String USER_NAME = "user_name";
public static final String USER_PASSWORD = "user_password";
}</span>
<span style="font-size:24px;">2.新建数据库帮助类,创建数据库及表</span>
<span style="font-size:14px;">import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqliteHelper extends SQLiteOpenHelper {
public MySqliteHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public MySqliteHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DataBaseUtils.DATABASE_NAME, null, DataBaseUtils.VERSON);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
//新建数据库表
String sql = "create table " + DataBaseUtils.TABLE_NAME + "(" + DataBaseUtils.USER_ID + " integer primary key," + DataBaseUtils.USER_NAME + " varchar(50)," + DataBaseUtils.USER_PASSWORD + " varchar(50) )";
db.execSQL(sql);
} catch (Exception e) {
// TODO: handle exception
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}</span>
<span style="font-size:24px;">3.数据库管理类</span>
<span style="font-size:14px;">import java.util.ArrayList;
import java.util.List;
import com.kashuo.bean.UserBean;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class DbManager {
private static MySqliteHelper helper;
/**
* 获取数据库帮助类对象
*
* @param context
* @return
*/
public static MySqliteHelper getInstance(Context context) {
if (helper == null) {
helper = new MySqliteHelper(context);
}
return helper;
}
/***
* 执行数据库语句
*
* @param db
* 数据库对象
* @param sql
* 语句
* @return 成功(true)与失败(false)
*/
public static boolean execDataSql(SQLiteDatabase db, String sql) {
try {
if (db != null) {
if (sql != null && !"".equals(sql)) {
db.execSQL(sql);
return true;
}
}
return false;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
/**
* 根据数据库sql查询
*
* @param db
* 数据库
* @param sql
* 语句
* @param selectionArgs
* 占位符取值字符数组
* @return 用户list集合
*/
public static List<UserBean> selectList(SQLiteDatabase db, String sql, String[] selectionArgs) {
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(sql, selectionArgs);
}
return CursorToList(cursor);
}
/**
* 将CURSOR转换为list
*
* @param cursor
* @return
*/
public static List<UserBean> CursorToList(Cursor cursor) {
List<UserBean> userlist = new ArrayList<UserBean>();
if (cursor != null) {
while (cursor.moveToNext()) {
UserBean user = new UserBean();
user.setUser_id(cursor.getString(cursor.getColumnIndex(DataBaseUtils.USER_ID)));
user.setUser_name(cursor.getString(cursor.getColumnIndex(DataBaseUtils.USER_NAME)));
user.setUser_password(cursor.getString(cursor.getColumnIndex(DataBaseUtils.USER_PASSWORD)));
userlist.add(user);
}
}
return userlist;
}
/***
* 查询数据库条数
*
* @param db
* 数据库
* @return 条数
*/
public static int getDataCount(SQLiteDatabase db) {
int count = 0;
if (db != null) {
Cursor cursor = db.rawQuery("select * from " + DataBaseUtils.TABLE_NAME, null);
count = cursor.getCount();
}
return count;
}
/**
* 关闭数据库
*
* @param db
*/
public static void sqliteClosed(SQLiteDatabase db) {
if (db != null) {
db.close();
}
}
}</span>
另附上数据库增删改查SQL语句:
增加:insert into 表名 (字段名,字段名)values (数据值,数据值)
删除:delete from 表名 where 字段名=数据值
修改:update 表名 set 字段=值 where 字段=值
查看:select * from 表名 where 字段=值