1

我正在创建一个应用程序,其中包含存储在 SQLite 数据库中的数据。我使用一个 long as 主键(生成购买 java.util.UUID 的有效位)。我使用扩展的 SQLiteOpenHelper 处理数据库连接。查询:

INSERT INTO Categorys VALUES('-7311183930966261019', 'Mat', 'Matkonto', '#FFFFFF', '#F23341', '0', '231451', '0', '0', '');

顺利通过,但是当我检索值时,第一个值 -7311183930966261019 中的很多精度都丢失了。查询

SELECT * FROM Categorys WHERE _id='-7311183930966261019';

返回空值;

我在 sqlite3 和命令行中尝试了相同的查询,但没有遇到同样的问题,所以我得出的结论是,问题要么与 android 相关,要么与我的处理方式有关。

更新:更正查询后,我现在有:

INSERT INTO Categorys VALUES(-6645129292311215613, 'Sparande', 'Pengar som sparas inför framtiden', '#FFFFFF', '#F23341', '0', '231451', '0', 0, '');

从数据库中我得到(toString() from my class):

Category: name: Sparande; description: Pengar som sparas inför framtiden; colorLight: #FFFFFF; colorDark: #F23341 _id: -6645129292311215104

检索数据的代码:

public static List<Category> readCategorys(Context context){
    TransactionSQLOH helper = new TransactionSQLOH(context, DATABASE_NAME);
    SQLiteDatabase db = helper.getReadableDatabase();
    Cursor cursor = db.query(CategorySQLOH.TABLE_NAME, CategorySQLOH.COLUMNS, null, null, null, null, null);
    List<Category> result = CategorySQLOH.cursorToCategory(cursor);
    helper.close();
    return result;
}
public static List<Category> cursorToCategory(Cursor cursor){
    List<Category> result = new Vector<Category>();
    if (cursor == null || cursor.getCount() == 0){
        return result;
    }
    cursor.moveToFirst();
    Category c;
    do{
        long _id = cursor.getLong(cursor.getColumnIndex(CategorySQLOH.COLUMNS[0]));
        String name = cursor.getString(cursor.getColumnIndex(CategorySQLOH.COLUMNS[1]));
        String description= cursor.getString(cursor.getColumnIndex(CategorySQLOH.COLUMNS[2]));
        String colorLight = cursor.getString(cursor.getColumnIndex(CategorySQLOH.COLUMNS[3]));
        String colorDark = cursor.getString(cursor.getColumnIndex(CategorySQLOH.COLUMNS[4]));
        int destribution = cursor.getInt(cursor.getColumnIndex(CategorySQLOH.COLUMNS[5]));
        long categoryGroup =  cursor.getLong(cursor.getColumnIndex(CategorySQLOH.COLUMNS[6]));
        int intData =  cursor.getInt(cursor.getColumnIndex(CategorySQLOH.COLUMNS[7]));
        long longData  = cursor.getLong(cursor.getColumnIndex(CategorySQLOH.COLUMNS[8]));
        String strData = cursor.getString(cursor.getColumnIndex(CategorySQLOH.COLUMNS[9]));
        c = new Category(_id, name, description, colorLight, colorDark, destribution, categoryGroup, intData, longData, strData);
        result.add(c);
    }while(cursor.moveToNext());
    return result;
}

更新:

SELECT * FROM 类别的结果;在提取的数据库上:

sqlite> SELECT * FROM Categorys;
-6.64512929231122e+18|Sparande|Pengar som sparas inf├Âr framtiden|#FFFFFF|#F2334
1|0|231451.0|0|0.0|

更新:

.schema 在拉取数据库上的结果。

CREATE TABLE Categorys ( _id REAL PRIMARY KEY, name TEXT NOT NULL, description T
EXT , colorLight TEXT , colorDark TEXT , destribution INTEGER , categoryGroup RE
AL , intData INTEGER , longData REAL , strData TEXT );
CREATE TABLE android_metadata (locale TEXT);
4

1 回答 1

1

Android: long => SQLiteDatabase => long ,精度丢失

我猜是因为您将其定义为LONG我认为您需要删除单引号,因为使用引号,SQLite 将其解释为几个字符而不是数字。

INSERT INTO Categorys VALUES(-7311183930966261019, 'Mat', ...);
SELECT * FROM Categorys WHERE _id = -7311183930966261019;

我自己测试过,一切正常。

于 2013-03-25T19:53:21.687 回答