我是Android新手,我还没有开发任何有关Android数据库的应用程序。所以我有几个基本问题。我是一个很好的数据库类样本,它将让我运行CRUD操作。我想将它用作静态类,如:
clsDB->Select("select * from clients");
或
objClient->Delete(clientid);
我想知道是否有人可以分享他的数据库课程,如果有更多人分享最好的。 另外,我还想要以多种方式重用这个课程,我将提供服务,活动等......
的问候,
Pentium10
答案 0 :(得分:1)
提供的SDK Notebook tutorial将引导您完成使用系统中提供的SQLite数据库ORM的基本知识。
答案 1 :(得分:1)
Dinedal的答案很好 - 记事本教程是您了解Android数据库处理的良好起点。
查看Android网站上的其他代码示例。
如果您想从各种活动和服务以简单一致的方式访问您的数据,您应该查看ContentProvider
文档。
还有一个名为SQLiteOpenHelper
的类,它允许您进行基本的SQLite数据库管理和版本控制。使用起来非常简单。
答案 2 :(得分:1)
你可以这样做:
DBAdapter.java :
public class DBAdapter {
private final int DATABASE_VERSION = 1;
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public static final String DATABASE_NAME = "db_title";
public static final String COLUMN_AUTO_INCREMENT_ID = "auto_increment_id";
private onDBListeners mDbListener;
public static final int actionGetAllData = 1, actionInsert = 2, actionUpdate = 3, actionDelete = 4, actionDeleteAll = 5;
private Object object = null;
public DBAdapter(Context ctx, onDBListeners listener) {
context = ctx;
DBHelper = new DatabaseHelper(context);
mDbListener = listener;
}
private class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_MY_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS Savedfiles");
onCreate(db);
}
}
// ---open database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes database---
public void close() {
DBHelper.close();
}
public boolean isValidCursor(Cursor cur){
try {
if(cur!=null){
if(cur.getCount()>0){
return true;
}else{
return false;
}
}else{
return false;
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
// --- Table for Data ---
public static final String TABLE_NAME = "tbl_my_table", COLUMN_USER_ID = "user_id", COLUMN_USERNAME = "username";
private static String[] GET_ALL_DATA() {
String[] getMessagesData = new String[] {COLUMN_AUTO_INCREMENT_ID, COLUMN_USER_ID,COLUMN_USERNAME};
return getMessagesData;
}
private static final String CREATE_MY_TABLE = "create table "
+ TABLE_NAME + "(" + COLUMN_AUTO_INCREMENT_ID + " integer primary key autoincrement, "
+ COLUMN_USER_ID + " text not null, "
+ COLUMN_USERNAME + " text);";
// ---insert data in database---
public void insertData(DataModel dataModel) {
open();
ContentValues initialValues = DataModel.getContentValues(dataModel);
boolean var = db.insert(TABLE_NAME, null, initialValues)>0;
if(var){
mDbListener.onOperationSuccess(TABLE_NAME, actionInsert, object);
}else{
mDbListener.onOperationFailed(TABLE_NAME, actionInsert);
}
close();
}
// ---get all data---
public void getAllData() {
open();
Cursor cur = db.query(TABLE_NAME, GET_ALL_DATA(), null, null,null, null, null);
if(cur!=null&&cur.getCount()>0){
mDbListener.onOperationSuccess(TABLE_NAME, actionGetAllData, cur);
}else{
mDbListener.onOperationFailed(TABLE_NAME, actionGetAllData);
}
cur.close();
close();
}
// ---get all data by id---
public void getAllDataByUserId(String userId) {
open();
Cursor cur = db.query(TABLE_NAME, GET_ALL_DATA(), COLUMN_USER_ID+ " = ?", new String[] {userId},null, null, null);
if (cur != null && cur.getCount() > 0) {
mDbListener.onOperationSuccess(TABLE_NAME, actionGetAllData, cur);
} else {
mDbListener.onOperationFailed(TABLE_NAME, actionGetAllData);
}
cur.close();
close();
}
// ---deletes all data---
public void deleteAllData() {
open();
boolean var = db.delete(TABLE_NAME, null, null)>0;
if(var){
mDbListener.onOperationSuccess(TABLE_NAME, actionDeleteAll, object);
}else{
mDbListener.onOperationFailed(TABLE_NAME, actionDeleteAll);
}
close();
}
// ---deletes single data---
public void deleteSingleData(String userId) {
open();
boolean var = db.delete(TABLE_NAME, COLUMN_USER_ID + "=" + userId, null) > 0;
if(var){
mDbListener.onOperationSuccess(TABLE_NAME, actionDelete, object);
}else{
mDbListener.onOperationFailed(TABLE_NAME, actionDelete);
}
close();
}
// ---updates a data details---
public void updateData(DataModel dataModel) {
open();
ContentValues initialValues = DataModel.getContentValues(dataModel);
boolean var = db.update(TABLE_NAME, initialValues, COLUMN_USER_ID + "="+ dataModel.getUserId(), null) > 0;
if(var){
mDbListener.onOperationSuccess(TABLE_NAME, actionUpdate, object);
}else{
mDbListener.onOperationFailed(TABLE_NAME, actionUpdate);
}
close();
}
}
添加 DataModel.java 类中的方法:
public static ContentValues getContentValues(DataModel dataModel){
ContentValues initialValues = new ContentValues();
if(dataModel.getUserId()!=null){
initialValues.put(DBAdapter.COLUMN_USER_ID, dataModel.getUserId());
}
if(dataModel.getUserName()!=null){
initialValues.put(DBAdapter.COLUMN_USERNAME, dataModel.getUserName());
}
return initialValues;
}
现在添加 界面 DbOperationListener :
public interface onDBListeners {
public void onOperationSuccess(String tableName, int operation, Object obj);
public void onOperationFailed(String tableName, int operation);
}
活动或片段中实施 界面:
public class MyActivity implements onDBListeners{
private DBAdapter mDbAdapter;
// Other stuff
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDbAdapter = new DBAdapter(MyActivity.this, this);
DataModel mDataModel = new DataModel();
mDataModel.setUserId("1");
mDataModel.setUserName("Hiren");
mDbAdpter.insertData(mDataModel);
}
@Override
public void onOperationSuccess(String tableName, int operation, Object obj) {
LogUtils.Log_e(TAG, "onOperationSuccess : Table Name - "+tableName+" : Table Operation - "+operation);
switch (operation) {
case DBAdapter.actionGetAllData:
Cursor cur = (Cursor) obj;
break;
case DBAdapter.actionInsert:
break;
case DBAdapter.actionUpdate:
break;
case DBAdapter.actionDelete:
break;
case DBAdapter.actionDeleteAll:
break;
default:
break;
}
}
@Override
public void onOperationFailed(String tableName, int operation) {
LogUtils.Log_e(TAG, "onOperationFailed : Table Name - "+tableName+" : Table Operation - "+operation);
switch (operation) {
case DBAdapter.actionGetAllData:
break;
case DBAdapter.actionInsert:
break;
case DBAdapter.actionUpdate:
break;
case DBAdapter.actionDelete:
break;
case DBAdapter.actionDeleteAll:
break;
default:
break;
}
}
}
就是这样。
答案 3 :(得分:0)
尝试使用ActiveAndroid。这是一个示例项目:https://www.activeandroid.com/help/sample-project/
答案 4 :(得分:0)
你可以查看android-active-record项目 http://code.google.com/p/android-active-record/ 虽然它更侧重于Java持久性模型,但您可以查看源代码和示例 - 您可以找到关于如何使用SQLite的大量有用示例