数据不会添加到SQL数据库中

时间:2012-04-01 09:33:03

标签: android sql eclipse save

SQL类的代码如下:

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class sqlDatabase {

public static final String KEY_ROWID = "_id";
public static final String KEY_PURCHASEREFUND = "_purchaserefund";  
public static final String KEY_LOCATION = "_location";
public static final String KEY_SHOPTYPE = "_shoptype";
public static final String KEY_PRICE = "_price";
public static final String KEY_DATE = "_date";
public static final String KEY_CARDUSED = "_cardused";

private static final String DATABASE_NAME = "receiptdb";
private static final String DATABASE_TABLE = "receipttable";
private static final int DATABASE_VERSION = 1;  

private receiptDBhelper receiptHelper;
private final Context receiptContext;
private SQLiteDatabase receiptDatabase;


private static class receiptDBhelper extends SQLiteOpenHelper{

    public receiptDBhelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }       

    @Override
    public void onCreate(SQLiteDatabase rdb) {
        // TODO Auto-generated method stub
        rdb.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_DATE + " TEXT NOT NULL, " +
                KEY_CARDUSED + " TEXT NOT NULL, " +
                KEY_LOCATION + " TEXT NOT NULL, " +
                KEY_SHOPTYPE + " TEXT NOT NULL, " +
                KEY_PRICE + " TEXT NOT NULL);"          
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase rdb, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        rdb.execSQL("DROP_TABLE_IF_EXISTS " + DATABASE_TABLE);
        onCreate(rdb);
    }

}

public sqlDatabase(Context c){
    receiptContext = c;
}

public sqlDatabase open() throws SQLException {
    receiptHelper = new receiptDBhelper(receiptContext);
    receiptDatabase = receiptHelper.getWritableDatabase();
    return this;        
}

public void close(){
    receiptHelper.close();
}

public long createEntry(String purchaserefund, String shoptype,
        String location, String price, String cardused) {
    // TODO Auto-generated method stub

    ContentValues dbcv = new ContentValues();

    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
    Date date = new Date();

    dbcv.put(KEY_PURCHASEREFUND, purchaserefund);
    dbcv.put(KEY_SHOPTYPE, shoptype);
    dbcv.put(KEY_LOCATION, location);
    dbcv.put(KEY_PRICE, price);
    dbcv.put(KEY_CARDUSED, cardused);
    dbcv.put(KEY_DATE, dateFormat.format(date));        

    return receiptDatabase.insert(DATABASE_TABLE, null, dbcv);
}

}

使用SQL类的类的相关代码:

String purchaserefund = precspurchaserefunds;
String shoptype = precsshoptypes;
String location = precsshoptypesselection[precsshoptypeselection];
String price = precsprices;
String cardused = precscarddetails[precscardusedselection];

sqlDatabase entry = new sqlDatabase(precs.this);

entry.open();

entry.createEntry(purchaserefund, shoptype, location, price, cardused);

entry.close();

据我所知,所有代码都在那里并且是正确的,但出于某种原因,当我查看SQL数据库浏览器时,没有插入值?

2 个答案:

答案 0 :(得分:0)

该代码中没有任何提交。这是我要寻找的第一件事,因为这是我在写入数据库或SharedPreferences时总是忘记的事情。 Lemme知道这不是解决方案。

答案 1 :(得分:0)

我认为问题实际上是你如何检查sql db内容。通常db文件存储在/ data / package_name / databases(或类似的东西,我不记得确切的路径),没有root访问权限,你不能只读取该文件夹中的文件。

所以使用SQL数据库浏览器可能就是问题..

尝试通过您的应用访问您的数据,看看数据是否存在。另外,请密切关注LogCat,可能存在与Db相关的一些警告/错误。

(PS。没有必要调用“commit”......)