2012-05-03 50 views
0

我開始學習開發Android應用程序。我想在android中開發一個本地語言字典。我的問題是如何創建一個數據庫,可以存儲詞彙(文本),音頻(發音)以及圖片?提前致謝。爲字典應用程序創建數據庫

回答

4

爲避免出現內存問題,您可以將音頻和圖像文件存儲在SD卡中,並將文件的名稱保存到數據庫中。當您需要這些文件時,只需從數據庫中獲取詳細信息並從SD卡播放/顯示。根據我的知識,這是最好的方法。

+0

謝謝您的回答,Krishnakant達哈爾... !!!你有什麼建議鏈接給我參考如何學習sqlite databese? –

+0

它的達拉爾。無論如何繼承人的鏈接http://www.vogella.com/articles/AndroidSQLite/article.html –

2

正常的SQLite數據庫足以讓你的應用程序存儲諸如音頻,圖片等的東西。 要清楚的是,如果您要創建原生應用程序,最好使用SD卡來存儲圖像和音頻文件,由此您可以根據需要檢索文件。

+0

如何檢索這些音頻文件和圖像?你能提供編碼嗎?謝謝。 –

1

SQLite將是一個合理的解決方案。

如果您需要連接到網絡數據庫,我建議在MySQL數據庫託管的服務器上使用PHP腳本。

下面是什麼一個SQLite數據庫看起來像一些示例代碼:

public class DatabaseHandler extends SQLiteOpenHelper { 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "android_api"; 

    // Login table name 
    private static final String TABLE_LOGIN = "login"; 

    // Login Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_NAME = "name"; 
    public static final String KEY_EMAIL = "email"; 
    private static final String KEY_UID = "uid"; 
    private static final String KEY_CREATED_AT = "created_at"; 

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

    // Creating Tables 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     String CREATE_LOGIN_TABLE = "CREATE TABLE " + TABLE_LOGIN + "(" 
       + KEY_ID + " INTEGER PRIMARY KEY," 
       + KEY_NAME + " TEXT," 
       + KEY_EMAIL + " TEXT UNIQUE," 
       + KEY_UID + " TEXT," 
       + KEY_CREATED_AT + " TEXT" + ")"; 
     db.execSQL(CREATE_LOGIN_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_LOGIN); 

     // Create tables again 
     onCreate(db); 
    } 

    /** 
    * Storing user details in database 
    * */ 
    public void addUser(String name, String email, String uid, String created_at) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_NAME, name); // Name 
     values.put(KEY_EMAIL, email); // Email 
     values.put(KEY_UID, uid); // Email 
     values.put(KEY_CREATED_AT, created_at); // Created At 

     // Inserting Row 
     db.insert(TABLE_LOGIN, null, values); 
     db.close(); // Closing database connection 
    } 

    public void addUser(String email) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_EMAIL, email); // Email 

     // Inserting Row 
     db.insert(TABLE_LOGIN, null, values); 
     db.close(); // Closing database connection 
    } 

    public String getUser() { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 

     String user = values.getAsString(KEY_EMAIL); 
     Log.v("DBH", user); 
     db.close(); 
     return user;   
    } 

    /** 
    * Getting user data from database 
    * */ 
    public HashMap<String, String> getUserDetails(){ 
     HashMap<String,String> user = new HashMap<String,String>(); 
     String selectQuery = "SELECT * FROM " + TABLE_LOGIN; 

     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(selectQuery, null); 
     // Move to first row 
     cursor.moveToFirst(); 
     if(cursor.getCount() > 0){ 
      user.put("name", cursor.getString(1)); 
      user.put("email", cursor.getString(2)); 
      user.put("uid", cursor.getString(3)); 
      user.put("created_at", cursor.getString(4)); 
     } 
     cursor.close(); 
     db.close(); 
     // return user 
     return user; 
    } 

    /** 
    * Getting user login status 
    * return true if rows are there in table 
    * */ 
    public int getRowCount() { 
     String countQuery = "SELECT * FROM " + TABLE_LOGIN; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     int rowCount = cursor.getCount(); 
     db.close(); 
     cursor.close(); 

     // return row count 
     return rowCount; 
    } 

    public String returnRows() { 
     String response = ""; 
     String countQuery = "SELECT * FROM " + TABLE_LOGIN; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     for (int j = 0; j < getRowCount(); j++) { 
      Cursor cursor = db.rawQuery(countQuery, null); 
      response += cursor.getColumnName(j); 
     } 
     Log.v("LT", response); 
     return response; 
    } 

    /** 
    * Re crate database 
    * Delete all tables and create them again 
    * */ 
    public void resetTables(){ 
     SQLiteDatabase db = this.getWritableDatabase(); 
     // Delete All Rows 
     db.delete(TABLE_LOGIN, null, null); 
     db.close(); 
    } 
+0

我正在開發一個離線字典。它可以仍然適用?謝謝。 –

+0

是的,看看addUser。 SQLiteDatabase db = this.getWritableDatabase(); ContentValues values = new ContentValues(); values.put(KEY_EMAIL,email); – Davek804

相關問題