使用Android中的数据库

时间:2014-01-17 12:55:13

标签: android database sqlite

我正在开发Android应用,我需要焦距(Brennweite)和传感器宽度(SensorBreite)参数。要使用此参数,我已创建了一个数据库。

我在SQLite数据库浏览器中使用了“CREATE TABLE智能手机(_id INTEGER PRIMARY KEY,模型TEXT,Brennweite FLOAT,SensorBreite FLOAT)”。

要打开数据库smartphone.db,DatenbankManager类会将其从资产复制到数据/数据。

public class DatenbankManager extends SQLiteOpenHelper {

private static String DB_PATH = "/data/data/de.fovea.waldinvent/databases/";
private static final String DB_NAME = "smartphone.db";
private static final int DB_VERSION = 1;

private final static String SMARTPHONE_TABLE = "smartphones";

private final static String SMARTPHONE_ID = "_id";
private final static String SMARTPHONE_MODEL = "Model";
private final static String SMARTPHONE_BRENNWEITE = "Brennweite";
private final static String SMARTPHONE_SENSORBREITE = "SensorBreite";

private SQLiteDatabase myDataBase;
private final Context myContext;

private static final String SMARTPHONE_CREATE =
        "CREATE TABLE smartphones (" +
        "_id INTEGER PRIMARY KEY, " +
        "Model TEXT NOT NULL, " + "Brennweite FLOAT, "+ 
        "SensorBreite FLOAT" +  ")";
    private static final String SMARTPHONE_DROP = 
        "DROP TABLE IF EXISTS smartphones";
    public static final String SMARTPHONE_SELECT_RAW =
        "SELECT _id, Model, Brennweite, SensorBreite FROM smartphones";

public DatenbankManager(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}

/**
 * Creates a empty database on the system and rewrites it with your own
 * database.
 * */
public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        // do nothing - database already exist
    } else {

        // By calling this method and empty database will be created into
        // the default system path
        // of your application so we are gonna be able to overwrite that
        // database with our database.
        this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each
 * time you open the application.
 * 
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
                SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {

        // database does't exist yet.

    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created
 * empty database in the system folder, from where it can be accessed and
 * handled. This is done by transfering bytestream.
 * */
private void copyDataBase() throws IOException {

    // Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_NAME);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException {

    // Open the database
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);

}

@Override
public synchronized void close() {

    if (myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(SMARTPHONE_CREATE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL(SMARTPHONE_DROP);
    onCreate(db);   }

在活动中,智能手机的型号保存为字符串。

String model = android.os.Build.MODEL;

现在,App必须在数据库中找到此模型字符串。

Cursor c = sqdatabase.rawQuery("SELECT * FROM smartphones WHERE Model = '"+model+"'", null); 
     c.moveToNext();

将Brennweite列保存在双f中,将SensorBreite保存在双B中。

我可以这样做吗?

3 个答案:

答案 0 :(得分:0)

是。 但我不认为SQLite有双数据类型,所以你将需要在SQLite中使用Numeric数据类型

答案 1 :(得分:0)

这些是您可以在SQLite中使用的类型,CANT在创建表时使用FLOAT

<强> NULL 即可。该值为NULL值。 INTEGER。该值是有符号整数,存储为1,2,3,4,6或8个字节,具体取决于值的大小。 的 REAL 即可。该值是浮点值,存储为8字节IEEE浮点数。 文字即可。该值是一个文本字符串,使用数据库编码(UTF-8,UTF-16BE或UTF-16LE)存储。 的 BLOB 即可。该值是一个数据块,完全按照输入的方式存储。

使用INTEGER代替FLOAT

答案 2 :(得分:0)

将字符串SMARTPHONE_CREATE更改为:

private static final String SMARTPHONE_CREATE =
        "CREATE TABLE smartphones (" +
        "_id INTEGER PRIMARY KEY, " +
        "Model TEXT NOT NULL, " + "Brennweite REAL, "+ 
        "SensorBreite REAL" +  ")";

为什么你想让BrenweiteSensorBreite浮动?如果值是十进制数,则使用REAL,否则使用INTEGER。