保存到SQL数据库中时,大串数字成为科学形式

时间:2018-10-18 16:11:38

标签: android database sqlite

我正在尝试将各种各样的大型数字字符串保存到sql数据库中,对于大约10个数字以上的数字,导出时是以科学形式显示的,例如3.5674E15。

下面的数字没有问题,但是我需要以精确的形式从数据库中导出值,例如45739473920284,而不是科学形式,因为丢失了最后几个数字。

这是我的数据库适配器:

public class DBAdapterWorkouts {
private static final String KEY_ROWID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_DATE = "date";
private static final String KEY_EXERCISESTRING = "exercisestring";
private static final String KEY_SUPERSETSTRING = "supersetstring";
private static final String TAG = "DBAdapter2";

private static final String DATABASE_NAME = "CustomWorkoutsDB";
private static final String DATABASE_TABLE = "customworkouts";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE = "create table if not exists customworkouts (id integer primary key autoincrement, " + "name string, date long, exercisestring string, supersetstring string);";

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapterWorkouts(Context ctx) {
    DBHelper = new DatabaseHelper(ctx);
}

//---opens the database---
public DBAdapterWorkouts open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
}

//---closes the database---
public void close() {
    DBHelper.close();
}

//---insert a record into the database---
public long insertRecord(String name, long date, String exercisestring, String supersetstring) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_EXERCISESTRING, exercisestring);
    initialValues.put(KEY_SUPERSETSTRING, supersetstring);
    return db.insert(DATABASE_TABLE, null, initialValues);
}

public void deleteName(int id){
    open();
    String query = "DELETE FROM " + DATABASE_TABLE + " WHERE "
            + KEY_ROWID + " = '" + id + "'";
    Log.d(TAG, "deleteName: query: " + query);
    db.execSQL(query);
    close();
}

//---deletes all records---
public void reset() throws SQLException {
    db.delete(DATABASE_TABLE, null,null);
    db.execSQL("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + DATABASE_TABLE + "'");
    this.DBHelper.onCreate(db);
}

//---retrieves all the records---
public Cursor getAllRecords() {
    return db.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_DATE, KEY_EXERCISESTRING, KEY_SUPERSETSTRING}, null, null, null, null, null, null);
}

//---retrieves a particular record---
public Cursor getRecord(long rowId) throws SQLException {
    Cursor mCursor =
            db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID, KEY_NAME, KEY_DATE, KEY_EXERCISESTRING, KEY_SUPERSETSTRING}, KEY_ROWID + "=" + rowId, null, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        try {
            db.execSQL(DATABASE_CREATE);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {

    }
}
}

这是在我的活动中,该数据是从数据库中保存和加载的:

            dbWorkouts.open();
            Cursor c = dbWorkouts.getAllRecords();
            boolean exists = false;
            try {
                if (c.moveToFirst()){
                    do {
                        if (c.getString(1).equals(etxtWorkoutName.getText().toString())){
                            exists = true;
                        }
                    }while (c.moveToNext());
                }
            } catch (Exception e){
                e.printStackTrace();
            }
            if (exists){
                Toast.makeText(getActivity(), "Please choose a workout name that doesn't already exist", Toast.LENGTH_LONG).show();
            }else{
                Calendar calendar = Calendar.getInstance();
                long date = calendar.getTimeInMillis();
                String name = etxtWorkoutName.getText().toString();
                dbWorkouts.insertRecord(name, date, stringExerciseOrder, stringSupersets);
                Toast.makeText(getActivity(), "Workout saved!", Toast.LENGTH_LONG).show();
                Log.w("app", stringExerciseOrder);
            }
            dbWorkouts.close();

在上述情况下,日志会生成我要保存的数字(一长串名为stringExerciseOrder的数字)。当我使用以下代码检索值时,就会出现问题。

dbWorkouts.open();
    Cursor c = dbWorkouts.getAllRecords();
    try {
        if (c.moveToFirst()){
            do {
                Log.w("app", c.getString(3));
            }while (c.moveToNext());
        }
    } catch (Exception e){
        e.printStackTrace();
    }
    dbWorkouts.close();

第二个日志的输出是科学形式。

任何帮助将不胜感激。

谢谢

1 个答案:

答案 0 :(得分:0)

//用此替换表创建命令。

 private static final String DATABASE_CREATE = "create table if not exists customworkouts (id integer primary key autoincrement, " + "name TEXT, date long, exercisestring TEXT, supersetstring TEXT);";

TEXT是SQLite中的类型,用于字符串格式。

List of SqLite data types

尝试并更新。