在特定文件夹android创建sqlite数据库

时间:2013-12-07 04:16:16

标签: android listview android-sqlite sqliteopenhelper

您好我有一个与数据库相关的问题

在我的项目中我需要在特定文件夹中创建数据库,所以任何伙伴都可以告诉我错误的地方

此类在/data/data/package_folder/database_name

中创建数据库

我想将其更改为sdcard/database_folder/database_name

请查看我的以下代码

    package com.example.qrcode.data;
    public class LocalDatabase extends SQLiteOpenHelper {

    public static final int DATABASE_VERSION = 1;

    // database name
    public static final String DATABASE_NAME = "qrcode_database";

    // table name
    public static final String TABLE_NAME = "scan_result";

    // table field name
    public static final String COLUMN_ID = "id";
    public static final String COLUMN_NAME = "scan_url";

    public static final String SAVE_TABLE_NAME = "DEVICE";

    public static final String SAVE_COLUMN_ID = "ID";
    public static final String SAVE_COLUMN_NAME = "DEVICE_ID";
    public static final String SAVE_COLUMN_KEY = "KEY";
    public static final String SAVE_COLUMN_VALUE = "VALUE";

    public static final String CREATE_SCAN_TABLE = "CREATE TABLE" + " "
            + TABLE_NAME + "(" + COLUMN_ID + " "
            + "INTEGER primary key autoincrement," + COLUMN_NAME + " TEXT,"
            + ");";

    public static final String CREATE_SAVE_TABLE = "CREATE TABLE" + " "
            + SAVE_TABLE_NAME + "(" + SAVE_COLUMN_ID
            + "INTEGER PRIMERY KEY AUTOINCREMENT" + SAVE_COLUMN_NAME + " TEXT"
            + SAVE_COLUMN_KEY + " TEXT" + SAVE_COLUMN_VALUE + " TEXT" + ");";

    public LocalDatabase(Context context, String name, CursorFactory factory,
            int version) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub

        try {
            db.execSQL(CREATE_SCAN_TABLE);
            db.execSQL(CREATE_SAVE_TABLE);
        } catch (Exception r) {
            r.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub

    }

    public void addname(String name) {

        SQLiteDatabase database = this.getWritableDatabase();

        Log.i("path ", database.getPath());
        ContentValues values = new ContentValues();
        // values.put(COLUMN_ID, scan_id);
        values.put(COLUMN_NAME, name);
        @SuppressWarnings("unused")
        Long int1 = database.insert(TABLE_NAME, null, values);
        database.close();

    }

    public void add_device(String data,
            ArrayList<HashMap<String, String>> jsonlist) {

        SQLiteDatabase database = this.getWritableDatabase();

        for (HashMap<String, String> map : jsonlist) {

            ContentValues values = new ContentValues();
            values.put(SAVE_COLUMN_NAME, data);
            values.put(SAVE_COLUMN_KEY, map.get(SAVE_COLUMN_KEY));
            values.put(SAVE_COLUMN_VALUE, map.get(SAVE_COLUMN_VALUE));
            @SuppressWarnings("unused")
            Long int1 = database.insert(SAVE_TABLE_NAME, null, values);

        }
        database.close();

        /*
         * for(HashMap<String, String> map : mylist){ ContentValues cv = new
         * ContentValues(); cv.put(FILE_NAME, map.get(FILE_NAME)); cv.put(DESC,
         * map.get(DESC)); cv.put(UPLOADED_BY, map.get(DATE_UPLOADED));
         * cv.put(ACTION, map.get(FILE_NAME)); cv.put(ID, map.get(ID));
         * cv.put(FILE_URI, map.get(FILE_URI)); db.insert("tablename", null,
         * cv); }
         */

    }

    public List<String> getemp() {

        List<String> name_list = new ArrayList<String>();

        String selectQuery = "SELECT " + SAVE_COLUMN_NAME + " FROM "
                + SAVE_TABLE_NAME;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {

                String name = cursor.getString(0);

                // Adding contact to list
                name_list.add(name);
            } while (cursor.moveToNext());
        }

        return name_list;

    }
}

提前致谢

1 个答案:

答案 0 :(得分:5)

首先,您必须指定SD卡的路径。您可以通过创建如下字符串来实现:

  public static final String  DATABASE_FILE_PATH = "/sdcard";

但你应该致电

    Environment.getExternalStorageDirectory()   

获取SD卡的根路径并使用它创建数据库。之后,您可以根据需要创建数据库。

这是一个例子

public class DatabaseHelper
{ 
   private static final String TAG  ="DatabaseHelper";

  public static final String  DATABASE_FILE_PATH = Environment.getExternalStorageDirectory();

   // database name
public static final String DATABASE_NAME = "qrcode_database";

// table name
public static final String TABLE_NAME = "scan_result";

// table field name
public static final String COLUMN_ID = "id";
public static final String COLUMN_NAME = "scan_url";

public static final String SAVE_TABLE_NAME = "DEVICE";

public static final String SAVE_COLUMN_ID = "ID";
public static final String SAVE_COLUMN_NAME = "DEVICE_ID";
public static final String SAVE_COLUMN_KEY = "KEY";
public static final String SAVE_COLUMN_VALUE = "VALUE";

public static final String CREATE_SCAN_TABLE = "CREATE TABLE" + " "
        + TABLE_NAME + "(" + COLUMN_ID + " "
        + "INTEGER primary key autoincrement," + COLUMN_NAME + " TEXT,"
        + ");";

public static final String CREATE_SAVE_TABLE = "CREATE TABLE" + " "
        + SAVE_TABLE_NAME + "(" + SAVE_COLUMN_ID
        + "INTEGER PRIMERY KEY AUTOINCREMENT" + SAVE_COLUMN_NAME + " TEXT"
        + SAVE_COLUMN_KEY + " TEXT" + SAVE_COLUMN_VALUE + " TEXT" + ");";

private SQLiteDatabase      database;

public DatabaseHelper() 
{  
    try
    {
        database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
            + File.separator + DATABASE_NAME, null,SQLiteDatabase.OPEN_READWRITE);
    }
    catch (SQLiteException ex)
    {
        Log.e(TAG, "error -- " + ex.getMessage(), ex);
        // error means tables does not exits
        createTables();
    }
    finally
    {
        DBUtil.safeCloseDataBase(database);
    }
}

private void createTables()
{
    database.execSQL(TRACKS_TABLE_CREATE);
    database.execSQL(TRACK_INFO_TABLE_CREATE);
}

public void close()
{
    DBUtil.safeCloseDataBase(database);
}

public SQLiteDatabase getReadableDatabase()
{
    database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
            + File.separator + DATABASE_NAME, null,
            SQLiteDatabase.OPEN_READONLY);
    return database;
}

public SQLiteDatabase getWritableDatabase()
{
    database = SQLiteDatabase.openDatabase(DATABASE_FILE_PATH
            + File.separator + DATABASE_NAME, null,
            SQLiteDatabase.OPEN_READWRITE);
    return database;
} 

最后,您必须在清单中设置权限,如下所示:

        android.permission.WRITE_EXTERNAL_STORAGE

希望对你有所帮助。

享受。