插入字符串值sqlite return null android

时间:2012-07-15 17:05:28

标签: android sqlite insert handler

我对此非常沮丧。 我尝试在我的数据库中放入一些字符串值,只有4个字段获取值: name =“O Teste 01”正确, total_time =“teste 1.gpx”错了, total_distance“00:10:00”错了, 日期“100”错误

以下代码:

我插入数据的地方:

            DatabaseHandler db = new DatabaseHandler(this);

        db.deleteStats(5);
        db.deleteStats(6);

        db.addStats(new Stats(  "O Teste 01", 
                                "teste 1.gpx", 
                                "00:10:00",
                                "100.00", 
                                "10.0",
                                "10.0", 
                                "20.0", 
                                "20", 
                                "1", 
                                "10", 
                                "100", 
                                "1", 
                                "2012-07-15 12:00:00"));    

Stats.java

public class Stats {
private Integer mId;
private String mName;
private String mFilename;
private String mTotalTime;
private String mTotalDistance;
private String mAvgSpeed;
private String mMinSpeed;
private String mMaxSpeed;
private String mAcumAltitude;
private String mMaxAltitude;
private String mMinAltitude;
private String mCalories;
private String mMod;
private String mDate;

// Construtor vazio
public Stats(){

}

// Construtor
public Stats(   Integer mId, String mName, String mFilename, String mTotalTime, String mTotalDistance, String mAvgSpeed, 
        String mMinSpeed, String mMaxSpeed, String mAcumAltitude, String mMinAltitude, String mMaxAltitude, 
                String mCalories, String mMod, String mDate)
{
    this.mId = mId;
    this.mName = mName;
    this.mFilename = mFilename;
    this.mTotalTime = mTotalTime;
    this.mTotalDistance = mTotalDistance;
    this.mAvgSpeed = mAvgSpeed;
    this.mMinSpeed = mMinSpeed;
    this.mMaxSpeed = mMaxSpeed;
    this.mAcumAltitude = mAcumAltitude;
    this.mMinAltitude = mMinAltitude;
    this.mMaxAltitude = mMaxAltitude;
    this.mCalories = mCalories;
    this.mMod = mMod;
    this.mDate = mDate;
}

// Construtor
public Stats(   String mName, String mFilename, String mTotalTime, 
                String mTotalDistance, String mAvgSpeed, String mMinSpeed, String mMaxSpeed, 
                String mAcumAltitude, String mMinAltitude, String mMaxAltitude, 
                String mCalories, String mMod, 
                String mDate)
{
    this.mName = mName;
    this.mFilename = mFilename;
    this.mTotalTime = mTotalTime;
    this.mTotalDistance = mTotalDistance;
    this.mAvgSpeed = mAvgSpeed;
    this.mMinSpeed = mMinSpeed;
    this.mMaxSpeed = mMaxSpeed;
    this.mAcumAltitude = mAcumAltitude;
    this.mMinAltitude = mMinAltitude;
    this.mMaxAltitude = mMaxAltitude;
    this.mCalories = mCalories;
    this.mMod = mMod;
    this.mDate = mDate;
}


public Stats(String string, String string2, String string3, String string4) {
    // TODO Auto-generated constructor stub
    this.mName = string;
    this.mFilename = string2;
    this.mTotalTime = string3;
    this.mDate = string4;
}

public Integer getmId() {
    return mId;
}
public void setmId(Integer mId) {
    this.mId = mId;
}
public String getmName() {
    return mName;
}
public void setmName(String mName) {
    this.mName = mName;
}
public String getmFilename() {
    return mFilename;
}
public void setmFilename(String mFilename) {
    this.mFilename = mFilename;
}
public String getmTotalTime() {
    return mTotalTime;
}
public void setmTotalTime(String mTotalTime) {
    this.mTotalTime = mTotalTime;
}
public String getmTotalDistance() {
    return mTotalDistance;
}
public void setmTotalDistance(String mTotalDistance) {
    this.mTotalDistance = mTotalDistance;
}
public String getmAvgSpeed() {
    return mAvgSpeed;
}
public void setmAvgSpeed(String mAvgSpeed) {
    this.mAvgSpeed = mAvgSpeed;
}
public String getmMinSpeed() {
    return mMinSpeed;
}
public void setmMinSpeed(String mMinSpeed) {
    this.mMinSpeed = mMinSpeed;
}
public String getmMaxSpeed() {
    return mMaxSpeed;
}
public void setmMaxSpeed(String mMaxSpeed) {
    this.mMaxSpeed = mMaxSpeed;
}
public String getmAcumAltitude() {
    return mAcumAltitude;
}
public void setmAcumAltitude(String mAcumAltitude) {
    this.mAcumAltitude = mAcumAltitude;
}
public String getmMaxAltitude() {
    return mMaxAltitude;
}
public void setmMaxAltitude(String mMaxAltitude) {
    this.mMaxAltitude = mMaxAltitude;
}
public String getmMinAltitude() {
    return mMinAltitude;
}
public void setmMinAltitude(String mMinAltitude) {
    this.mMinAltitude = mMinAltitude;
}
public String getmCalories() {
    return mCalories;
}
public void setmCalories(String mCalories) {
    this.mCalories = mCalories;
}
public String getmMod() {
    return mMod;
}
public void setmMod(String mMod) {
    this.mMod = mMod;
}
public String getmDate() {
    return mDate;
}
public void setmDate(String mDate) {
    this.mDate = mDate;
}

将数据插入sqlite的功能:

    public void addStats(Stats st) 
{
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(DatabaseHandler.COLUMN_NAME, st.getmName());
    values.put(DatabaseHandler.COLUMN_FILENAME, st.getmFilename());
    values.put(DatabaseHandler.COLUMN_TOTAL_TIME, st.getmTotalTime());
    values.put(DatabaseHandler.COLUMN_TOTAL_DISTANCE, st.getmTotalDistance());
    values.put(DatabaseHandler.COLUMN_AVG_SPEED, st.getmAvgSpeed());
    values.put(DatabaseHandler.COLUMN_MIN_SPEED, st.getmMinSpeed());
    values.put(DatabaseHandler.COLUMN_MAX_SPEED, st.getmMaxSpeed());
    values.put(DatabaseHandler.COLUMN_ACUM_ALTITUDE, st.getmAcumAltitude());
    values.put(DatabaseHandler.COLUMN_MIN_ALTITUDE, st.getmMinAltitude());
    values.put(DatabaseHandler.COLUMN_MAX_ALTITUDE, st.getmMaxAltitude());
    values.put(DatabaseHandler.COLUMN_CALORIES, st.getmCalories());
    values.put(DatabaseHandler.COLUMN_MOD, st.getmMod());
    values.put(DatabaseHandler.COLUMN_DATE, st.getmDate());

    // Adiciona linha
    db.insert(DatabaseHandler.TABLE_STATS, null, values);
    db.close(); // fechando a conexão à base de dados
} 

提前致谢!

这是代码的其余部分......

public class DatabaseHandler extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 4;

// Database Name
private static final String DATABASE_NAME = "magik_tracker_gpx";

// estatísticas table name
private static final String TABLE_STATS = "stats";

// estatísticas Table Columns names
public static final String COLUMN_ID = "_id";
public static final String COLUMN_NAME = "name";
public static final String COLUMN_FILENAME = "filename";
public static final String COLUMN_TOTAL_TIME = "total_time";
public static final String COLUMN_TOTAL_DISTANCE = "total_distance";
public static final String COLUMN_AVG_SPEED = "avg_speed";  
public static final String COLUMN_MIN_SPEED = "min_speed";  
public static final String COLUMN_MAX_SPEED = "max_speed";  
public static final String COLUMN_ACUM_ALTITUDE = "acum_altitude";  
public static final String COLUMN_MIN_ALTITUDE = "min_altitude";    
public static final String COLUMN_MAX_ALTITUDE = "max_altitude";    
public static final String COLUMN_CALORIES = "calories";    
public static final String COLUMN_MOD = "mod";  
public static final String COLUMN_DATE = "date";

private static final String DATABASE_CREATE = "create table "
        + TABLE_STATS + " ( " 
        + COLUMN_ID + " integer primary key autoincrement, " 
        + COLUMN_NAME + " text," 
        + COLUMN_FILENAME   + " text, "         
        + COLUMN_TOTAL_TIME + " text, " 
        + COLUMN_TOTAL_DISTANCE + " text, " 
        + COLUMN_AVG_SPEED + " text, " 
        + COLUMN_MIN_SPEED + " text, " 
        + COLUMN_MAX_SPEED + " text, " 
        + COLUMN_ACUM_ALTITUDE + " text, " 
        + COLUMN_MIN_ALTITUDE + " text, " 
        + COLUMN_MAX_ALTITUDE + " text, " 
        + COLUMN_CALORIES + " text ," 
        + COLUMN_MOD + " text, " 
        + COLUMN_DATE + " text " 
        + ");"; 

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(DATABASE_CREATE);
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_STATS);

    // Create tables again
    onCreate(db);
}

1 个答案:

答案 0 :(得分:0)

好的,我的坏......

我没有得到所有的领域!哎呀

非常感谢

 public List<Stats> getAllStats() {
    List<Stats> statsList = new ArrayList<Stats>();
    // Select All Query
    String selectQuery = "SELECT  * FROM " + TABLE_STATS;

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

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            Stats linha = new Stats();
            linha.setmId(Integer.parseInt(cursor.getString(0)));
            linha.setmName(cursor.getString(1));
            linha.setmFilename(cursor.getString(2));
            linha.setmTotalDistance(cursor.getString(3));
            linha.setmAvgSpeed(cursor.getString(4));
            linha.setmMinSpeed(cursor.getString(5));
            linha.setmMaxSpeed(cursor.getString(6));
            linha.setmAcumAltitude(cursor.getString(7));
            linha.setmMinAltitude(cursor.getString(8));
            linha.setmMaxAltitude(cursor.getString(9));
            linha.setmCalories(cursor.getString(10));
            linha.setmMod(cursor.getString(11));
            linha.setmDate(cursor.getString(12));
            // Adding contact to list
            statsList.add(linha);
        } while (cursor.moveToNext());
    }

    // return estatísticas list
    return statsList;
}