2016-01-16 30 views
3

這是我的dbhelper代碼。我想問一下在這個數據庫中是否可以支持多種語言?是否需要使用谷歌翻譯API或應該爲另一種語言創建另一個數據庫?將數據庫幫助程序更改爲支持多種語言的方法?

private DatabaseHelper dbHelper; 
private SQLiteDatabase db; 

private static final String K_ID = "ID"; 
private static final String K_NAME = "NAME"; 
private static final String K_BEN = "BENEFIT"; 
private static final String TABLE = "PLANT"; 

private static class DatabaseHelper extends SQLiteOpenHelper { 
    DatabaseHelper(Context context) { 
     super(context, "DBPLANT", null, 1); 
    } 

    public void onCreate(SQLiteDatabase db) { 

     String sql = "CREATE TABLE " + TABLE + " (" + K_ID 
       + " INTEGER PRIMARY KEY ," + K_NAME + " TEXT , " + K_BEN 
       + " TEXT);"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('ALOE (Aloe Vera)','This plant has hundreds of uses, the most popular being its ability to alleviate the pain of burns and to speed their healing. Immediately immerse the burn in cold water or apply ice until the heat subsides, then generously apply the aloeAloe may also be applied to any cut or skin abrasion, and onto skin eruptions, remarkably speeding healing. To relieve the pain and itching of hemorrhoids, carve out a suppository sized chunk of the inner leaf gel and insert into the rectum.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('GARLIC (Allium sativum)','Best known for its antibiotic effect, garlic bulbs or the milder garlic greens can be eaten raw at the onset of a cold or flu. Garlic oil is effectively used for ear infections. It is easily made by finely chopping enough fresh organic garlic bulbs to fill a jelly jar, and covering them with organic olive oil.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('GINGER (Zinziber officiale)','Ginger has a carminative effect, which means that it will help relieve digestive problems which result in gas formation. It is also a diaphoretic, used both as a tea and added to a soaking bath to stimulate sweating and reduce fevers.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('COCONUT','White meat and water from the cavity are used for heart conditions, dysentery, fever, pain, and digestive and bladder problems, to quench thirst and as an aphrodisiac. To treat diarrhea, meat from young fruits is mixed with other ingredients and rubbed onto the stomach. Oil prepared from boiling coconut milk is thought of as antiseptic and soothing and so is smoothed onto the skin to treat burns, ringworm and itching.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Carallia Brachiata','The bark was extracted with petroleum ether, ethyl acetate and methanol successively. All the extracts were screened for wound healing activity by excision and incision models in Wistar rats. The ethyl acetate and methanol extracts were found to possess significant wound healing activity. The extracts revealed the presence of sterols or triterpenoids, flavonoids, phenols, tannins, carbohydrates, fixed oils and fats.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Ficus Hispida','The fruits are bitter, refrigerant, astringent, acrid, anti-dysenteric, anti-inflammatory, depurative, vulnerary, haemostatic and galactagogue. They are useful in ulcere, leucoderma, psoriasis, anaemia, haemorrhoids, jaundice, epistaxis, stomatorrhagia, inflammations, intermittent fever and vitiated conditions of pitta.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Leea Indica','A decoction of the root is given in colic, is cooling and relieves thirst. In Goa, the root is much used in diarrheal and chronic dysentery. The roasted leaves are applied to the head in vertigo. The juice of the young leaves is a digestive. Plant pacifies vitiated pitta, diarrhea, dysentery, colic, ulcers, skin diseases, and vertigo.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Mesua Ferrea','Flowers are acrid, anodyne, digestive, constipating, and stomachic. They are used in treating asthma, leprosy, cough, fever, vomiting and impotency. The seed oil pacifies vata, and also good for skin diseases and rheumatism');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Trema Orientalis','It has been used for medicinal purposes including the treatment of respiratory, inflammatory, and helminthic diseases. Almost every part of the plant is used as medicine in various parts of Africa.');"; 
     db.execSQL(sql); 
     sql = "INSERT INTO " 
       + TABLE 
       + " (" 
       + K_NAME 
       + "," 
       + K_BEN 
       + ") VALUES('Murraya Paniculata','The decoction of the leaves can be used as a gargle to treat toothache. The leaves are frequently used to treat pain due to scalding. This decoction can be given orally to treat body aches, as a tonic, and for expelling tape worm');"; 
     db.execSQL(sql); 
    } 

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     db.execSQL("DROP TABLE IF EXISTS " + TABLE); 
     onCreate(db); 
    } 
} 

/** 
* Upgrade database 
*/ 
public void Reset() { 
    dbHelper.onUpgrade(this.db, 1, 1); 
} 

/** 
* Constructor 
* 
* @param ctx 
*   the activity context 
*/ 
public DBHelper(Context ctx) { 
    dbHelper = new DatabaseHelper(ctx); 
} 

/** 
* Open database connection 
* 
* @return the database connection 
* @throws SQLException 
*/ 
public DBHelper open() throws SQLException { 
    db = dbHelper.getWritableDatabase(); 
    return this; 
} 

/** 
* Close database connection 
*/ 
public void close() { 
    dbHelper.close(); 
} 

public boolean createEntry(String name, String benefit) { 
    ContentValues cv = new ContentValues(); 
    cv.put(K_NAME, name); 
    cv.put(K_BEN, benefit); 
    return db.insert(TABLE, null, cv) != -1; 
} 

public boolean updateEntry(String name, String benefit, String id) { 
    ContentValues cv = new ContentValues(); 
    cv.put(K_NAME, name); 
    cv.put(K_BEN, benefit); 
    return db.update(TABLE, cv, K_ID + " = ?", new String[] { id }) > 0; 
} 

public PlantList getList() { 
    PlantList plants = new PlantList(); 
    Cursor cur = db.rawQuery("SELECT " + K_ID + " ," + K_NAME + " FROM " 
      + TABLE, null); 

    if (cur.moveToFirst()) { 
     do { 
      plants.addData(cur.getString(cur.getColumnIndex(K_ID)), 
        cur.getString(cur.getColumnIndex(K_NAME))); 
     } while (cur.moveToNext()); 
    } 
    cur.close(); 
    return plants; 
} 

public PlantList getQuery(String query) { //search data 
    PlantList plants = new PlantList(); 
    Cursor cur = db.rawQuery("SELECT " + K_ID + " ," + K_NAME + " FROM " 
      + TABLE + " WHERE " + K_NAME + " LIKE '%" + query + "%'", null); 

    if (cur.moveToFirst()) { 
     do { 
      plants.addData(cur.getString(cur.getColumnIndex(K_ID)), 
        cur.getString(cur.getColumnIndex(K_NAME))); 
     } while (cur.moveToNext()); 
    } 
    cur.close(); 
    return plants; 
} 

public String[] getDetail(String id) { 
    String data[] = new String[2]; 
    Cursor cur = db.query(TABLE, new String[] { K_NAME, K_BEN }, K_ID + "=" 
      + id, null, null, null, null); 

    if (cur.moveToFirst()) { 
     data[0] = cur.getString(cur.getColumnIndex(K_NAME)); 
     data[1] = cur.getString(cur.getColumnIndex(K_BEN)); 
    } 
    cur.close(); 
    return data; 
} 

public String getName(String id) { 
    String name = null; 
    Cursor cur = db.query(TABLE, new String[] { K_NAME }, K_ID + "=" + id, 
      null, null, null, null); 

    if (cur.moveToFirst()) { 
     name = cur.getString(cur.getColumnIndex(K_NAME)); 
    } 
    cur.close(); 
    return name; 
} 

public boolean deleteEntry(String id) { 
    return db.delete(TABLE, K_ID + " = ?", new String[] { id }) > 0; 
} 

}

回答

3

是的,你可以使用相同的數據庫SQLite總是使用Unicode創建數據庫時指定的Unicode編碼將文本數據存儲爲Unicode。數據庫驅動程序本身負責將數據作爲Unicode語言/平臺使用的編碼返回。

內部sqlite對UTF-8或UTF-16(每個數據庫都有一個選項)的所有字符串進行編碼,但是因爲它總是將它們根據需要進行轉換(將其轉換爲一個或多個用於比較和/從java.lang .String在API中),你甚至不需要關心。

在數據庫表中,您可以爲不同語言的語言字符串添加不同列,並僅從該特定列中獲取數據。

在您的應用程序上,您可以提供用戶更改語言的選項。那時您可以根據所選語言更改列,並僅對該列添加/編輯操作。

+0

好的。那麼我的下一個問題將是如何編寫用於在數據庫中更改語言的代碼。對於我的應用程序,用戶可以添加/編輯數據庫,如果應用程序在運行時,用戶添加新數據,我不知道如何獲得新的數據翻譯它在另一種語言.. –

+0

我的更新回答是否回答你的問題? –

+0

你的意思是說,如果一個用戶鍵入英文單詞「En」數據,那麼列(英文)將收集數據,用戶只能在列中看到它(英文)?如果用戶更改了「En」語言改爲其他語言,用戶無法在其他語言欄中看到「En」數據? –

1

而不是創建一個單獨的數據庫中,你可能會更好過增加另一列表中顯示的語言,只是存儲在同一個表中的本地化字符串。

+0

你有這樣做的例子嗎? –

+0

這裏有一些有用的討論:http://stackoverflow.com/questions/316780/schema-for-a-multilanguage-database –