我的应用程序需要在同一个数据库下使用多个表。我正在为每个表开发一个全局适配器和单独的适配器来实现CRUD。请参阅数据库适配器,如下所示:
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public abstract class BahiKhataDbAdapter {
protected static final String TAG = "BahiKhataDbAdapter";
protected DatabaseHelper mDbHelper;
protected SQLiteDatabase mDb;
private static final String DATABASE_NAME = "bahikhatadata.db";
private static final int DATABASE_VERSION = 1;
protected static final String TABLE_EXPENSE_TYPE =
"create table expensetype (_id integer primary key autoincrement, "
+ "expense_type text not null);";
protected static final String TABLE_INCOME_TYPE =
"create table incometype (_id integer primary key autoincrement, "
+ "income_type text not null);";
protected static final String TABLE_GROUP =
"create table group (_id integer primary key autoincrement, "
+ "group_name text not null);";
protected static final String TABLE_EXPENSE =
"create table expensetype (_id integer primary key autoincrement, "
+ "expense_amt real not null, note text, expense_type text not null, group
text not null, expense_date text not null);";
protected static final String TABLE_INCOME =
"create table expensetype (_id integer primary key autoincrement, "
+ "income_amt real not null, note text, income_type text not null, group
text not null, income_date text not null);";
protected final Context mCtx;
protected static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper (Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(TABLE_EXPENSE_TYPE);
db.execSQL(TABLE_INCOME_TYPE);
db.execSQL(TABLE_GROUP);
db.execSQL(TABLE_EXPENSE);
db.execSQL(TABLE_INCOME);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS expensetype");
db.execSQL("DROP TABLE IF EXISTS incometype");
db.execSQL("DROP TABLE IF EXISTS group");
db.execSQL("DROP TABLE IF EXISTS expense");
db.execSQL("DROP TABLE IF EXISTS income");
onCreate(db);
}
}
public BahiKhataDbAdapter(Context ctx) {
this.mCtx = ctx;
}
public BahiKhataDbAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
}
和费用类型的适配器如下:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
public class ExpenseTypeDbAdapter extends BahiKhataDbAdapter {
// Database fields
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "expense_type";
private static final String DB_TABLE = "expensetype";
public ExpenseTypeDbAdapter(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
/**
* Create a new business If the business is successfully created return the new
* rowId for that note, otherwise return a -1 to indicate failure.
*/
public long createExpenseType(String name) {
//ContentValues values = createContentValues(name);
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
return mDb.insert(DB_TABLE, null, args);
}
/*private ContentValues createContentValues(String expense_type) {
ContentValues values = new ContentValues();
values.put(KEY_NAME, expense_type);
return values;
}*/
/**
* Deletes expensetype record
*/
public boolean deleteExpenseType(String expense_type) {
return mDb.delete(DB_TABLE, "KEY_NAME=?", new String[] {expense_type}) > 0;
}
/**
* Return a Cursor over the list of all todo in the database
*
* @return Cursor over all notes
*/
public Cursor fetchAllExpenseType() {
return mDb.query(DB_TABLE, new String[] { KEY_ROWID, KEY_NAME,}, null, null, null, null, null);
}
public long count() {
return DatabaseUtils.queryNumEntries(mDb, DB_TABLE);
}
}
并在我的一个活动中使用ExpenseTypeDbAdapter,如下所示:(在此活动中,我使用Spinner填充expensetype表的数据。请参阅以下代码:
import android.app.Activity;
//import android.app.AlertDialog;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class ExpenseTypeList extends Activity implements OnClickListener ,
OnItemSelectedListener{
/** Properties **/
protected Spinner expenseSpinner;
protected Button expenseDone;
protected Button addNewExpenseType;
protected ExpenseTypeDbAdapter dbHelper;
protected String expenseType;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expense);
dbHelper = new ExpenseTypeDbAdapter(this);
//configure the widgets
addNewExpenseType = (Button) findViewById(R.id.add_new_expense_type);
expenseDone = (Button) findViewById(R.id.expense_done);
// Setup ClickListeners
addNewExpenseType.setOnClickListener(this);
expenseDone.setOnClickListener(this);
// initialize the database
dbHelper.open();
expenseSpinner = (Spinner) findViewById(R.id.expense_spinner);
//populated the spinner from the ExpenseData db
if(dbHelper.count() == 0) {
dbHelper.createExpenseType("Jan");
dbHelper.createExpenseType("Feb");
dbHelper.createExpenseType("Mar");
dbHelper.createExpenseType("Apr");
}
getSpinnerData();
expenseSpinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,
long id) {
// TODO Auto-generated method stub
if(spinner == expenseSpinner) {
// get the expense type
Cursor cursor = (Cursor) spinner.getSelectedItem();
expenseType = cursor.getString(1);
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v == expenseDone) {
//Toast.makeText(this, expenseType + " selected", Toast.LENGTH_LONG).show();
Uri data = Uri.parse(expenseType);
Intent result = new Intent(null, data);
result.putExtra("EXPENSE_TYPE", expenseType);
setResult(RESULT_OK, result);
finish();
}
if(v == addNewExpenseType){
Toast.makeText(this, expenseType + " selected", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, AddExpenseType.class);
startActivity(intent);
}
}
public void getSpinnerData(){
Cursor cursor = dbHelper.fetchAllExpenseType();
String[] from = new String[] {ExpenseTypeDbAdapter.KEY_NAME}; //use class name directly
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter expenseCursorAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_spinner_item,
cursor,
from,
to
);
expenseSpinner.setAdapter(expenseCursorAdapter);
expenseCursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (dbHelper != null) {
dbHelper.close();
}
}
}
ExpenseTypeList不起作用;它给出了SQLite = 1的错误;创建组表时出现语法错误。评论&#34;创建小组&#34;语法并从Android模拟器中卸载应用程序;但仍然得到错误。
有人可以帮我解决这个问题。提前致谢。
答案 0 :(得分:1)
“group”是关键字,请尝试重命名您的表格。 TABLE_EXPENSE_TYPE和TABLE_EXPENSE具有相同的表名。