我是 SQlite 数据库的新手,我想制作 2 个表:
1) my_folders(
folder_id INTEGER PRIMARY KEY AUTOINCREMENT,
folder_tite TEXT NOT NULL
currency TEXT
folder_descrip TEXT
folder_pic TEXT
);
2) TABLE my_expenses(
log_id INTEGER PRIMARY KEY AUTOINCREMENT,
e_folder_id INTEGER
name TEXT NOT NULL,
amount INTEGER
date NUMERIC NOT NULL
time NUMERIC NOT NULL
notes TEXT
FOREIGN KEY(e_folder_id) REFERENCES my_folders(folder_id)
);
我的问题是如何实现第二张表?我可以直接添加代码以在同一个 DBAdapter 文件中创建第二个表“my_expenses”吗?或者我应该像https://stackoverflow.com/a/5899110/1398267这样的不同文件中的所有表格分开?如果是这种情况,在 my_folders 和 my_expenses 表中插入新记录所需的代码是什么?
我已经创建了 1 个表,可以通过这段代码向表中添加新记录:
Button b1 = (Button) findViewById(R.id.bnext);
b1.setOnClickListener(new OnClickListener()
{ public void onClick(View v)
{
db.open();
db.insertFolder(getFolderTitle.getText().toString(),
getCurrency.getSelectedItem().toString(), getFolderDescription.getText().toString());
db.close();
}
}
这是我的数据库代码:
public class DBAdapter
{
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_CURRENCY = "currency";
public static final String KEY_NOTES = "notes";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "MyDB";
private static final String DATABASE_TABLE = "my_folders";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table my_folders (_id integer primary key autoincrement, "
+ "name text not null, currency text not null, notes text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
try
{
db.execSQL(DATABASE_CREATE);
}
catch (SQLException e)
{
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS my_folders");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close()
{
DBHelper.close();
}
// ---insert a folder into the database---
public long insertFolder(String name, String currency, String notes)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, name);
initialValues.put(KEY_CURRENCY, currency);
initialValues.put(KEY_NOTES, notes);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// ---deletes a particular folder---
public boolean deleteFolder(long rowId)
{
return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
// ---deletes all folders---
public boolean deleteAll()
{
return db.delete(DATABASE_TABLE, "1", null) > 0;
}
// ---retrieves all the folders---
public Cursor getAllFolders()
{
return db.query(DATABASE_TABLE, new String[]
{KEY_ROWID, KEY_NAME, KEY_CURRENCY, KEY_NOTES}, null, null, null, null, null);
}
// ---retrieves a particular folder---
public Cursor getFolder(long rowId) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[]
{ KEY_ROWID, KEY_NAME, KEY_CURRENCY, KEY_NOTES },
KEY_ROWID + "=" + rowId, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
// ---updates a folder---
public boolean updateFolder(long rowId, String name, String currency, String notes)
{
ContentValues args = new ContentValues();
args.put(KEY_NAME, name);
args.put(KEY_CURRENCY, currency);
args.put(KEY_NOTES, notes);
return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
}