0

我是 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;
    }
}
4

2 回答 2

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)");
}

这是我前段时间做的一个项目的一个简短例子。这些是定义创建表查询的常量(正如您已经为第一个表所做的那样)。

只需编写新的创建查询就可以了。

于 2012-06-07T08:01:45.970 回答
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,然后您可以添加类似的代码来添加第二个表中的所有值。

于 2012-06-07T09:10:29.663 回答