0

我已经成功地创建了一个数据库程序。基本上我正在处理一个有点复杂的数据库,所以需要在 SQLITE MANAGER 中创建数据库并使用我的 android 代码导入/链接该数据库。请建议我教程来做到这一点。或任何其他对我有帮助的代码。谢谢

这是我的数据库代码现在我想在其中导入 mYdataBase.sqlite 文件。

public class DatabaseHandler extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "contactsManager";
    private static final String TABLE_CONTACTS = "contacts";
    private static final String KEY_ID = "id";
    private static final String KEY_NAME = "name";
    private static final String KEY_PH_NO = "phone_number";

    public DatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_PH_NO + " TEXT" + ")";
        db.execSQL(CREATE_CONTACTS_TABLE);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);

        // Create tables again
        onCreate(db);
    }
4

2 回答 2

5

如果你想从资产文件夹中导入你的数据库文件,下面的代码可以帮助你

void checkDB() throws Exception {
        try {
            SQLiteDatabase dbe = SQLiteDatabase
                    .openDatabase(
                            "/data/data/com.yourpackagename/databases/yourfile.sqlite",
                            null, 0);
            Log.d("opendb", "EXIST");
            dbe.close();
        } catch (Exception e) {

            AssetManager am = getApplicationContext().getAssets();
            OutputStream os = new FileOutputStream(
                    "/data/data/com.yourpackagename/databases/yourfile.sqlite");
            byte[] b = new byte[100];

            int r;
            InputStream is = am.open("yourfile.sqlite");
            while ((r = is.read(b)) != -1) {
                os.write(b, 0, r);
            }
            Log.i("DATABASE_HELPER", "Copying the database ");
            is.close();
            os.close();
        }
于 2012-09-05T17:48:03.610 回答
0

尝试使用这个http://codinglookseasy.blogspot.in/2012/08/sqlite-database.html。这是在应用程序中使用数据库的示例。希望这对你有帮助

于 2012-09-05T17:45:15.693 回答