• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

android - 更改数据库助手以支持多种语言的方法?

[复制链接]
菜鸟教程小白 发表于 2022-12-9 06:20:11 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的 dbhelper 代码。我想问一下这个数据库是否可以支持多种语言?是否需要使用 google translate 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 = "LANT";

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

}



Best Answer-推荐答案


是的,您可以使用相同的数据库 SQLite 始终将文本数据存储为 Unicode,使用创建数据库时指定的 Unicode 编码。数据库驱动程序本身负责将数据作为您的语言/平台使用的编码中的 Unicode 字符串返回。

sqlite 在内部以 UTF-8 或 UTF-16 对所有字符串进行编码(有一个针对每个数据库的选项),但因为它总是根据需要转换它们(转换为一个或另一个以进行比较以及与 java.lang.lang 之间的转换。 API 中的字符串),你甚至不需要关心。

在您的数据库表中,您可以为不同语言的语言字符串添加不同的列,并仅从该特定列获取数据。

在您的应用程序上,您可以为用户提供更改语言的选项。那时您可以根据所选语言更改列并仅对该列执行添加/编辑操作。

关于android - 更改数据库助手以支持多种语言的方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34824537/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap