我是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;
}
}
答案 0 :(得分:2)
您只需在onCreate方法中执行更多查询:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DBINFO); //create table db_info as...
db.execSQL(CREATE_TYPE); //create table ...
db.execSQL(CREATE_TAG); //create table ... all are constant strings!
db.execSQL(CREATE_CATEGORY);
db.execSQL(CREATE_RESOURCE);
db.execSQL(CREATE_RESTAG);
db.execSQL(CREATE_RESCAT);
db.execSQL(CREATE_TYPECAT);
db.execSQL("insert into DBInfo values(-1,0)");
db.execSQL("insert into Tag values(1,'No tag',0)");
}
这是我前一段时间做过的一个简短的例子。 这些是定义create table查询的常量(确切地说,您已经为第一个表执行了这一操作)。
只需编写新的创建查询,就可以了。
答案 1 :(得分:1)
是的,您可以在同一个文件中添加代码。 首先从您要添加到第二个表中的my_folders中获取folder_id,并将其存储在某个类变量中。 您可以使用将返回folder_id的函数。
public int getFolder_id(String name) throws SQLException
{
int id =0;
Cursor cursor = db.rawQuery("select _id from my_folders where name = ?", new String[] { name });
if (mCursor != null)
{
mCursor.moveToFirst();
id = cursor.getInt(cursor.getColumnIndex("_id"));
}
return id;
}
之后检查该变量是否不是0或-1然后你可以添加类似的代码来添加第二个表中的所有值。