在使用创建表pm.createTable()
并向其添加数据后pm.addDataToTable()
,我试图使用pm.getData()
我刚刚创建和插入的同一个表检索数据,并将其存储在游标c中。我没有收到任何编译错误或异常。控件总是在 中进入 else 条件displayData()
。有人可以帮我弄这个吗?
PortfolioManager pm;
Cursor c, cMainTable;
pm = new PortfolioManager(this);
cMainTable = pm.getData("mainTable");
scriptName = edtxtScriptName.getText().toString();
try
{
System.out.println("Creating table with name "+scriptName);
pm.createTable(scriptName);
System.out.println("Table created");
pm.addDataToTable(scriptName, DOP, purchasedQuantity, purchasedPrice, brokerage, serviceTax, stt, stampDuty);
System.out.println("data added");
c = pm.getData(scriptName);
displayData(c);
}
catch (Exception e) {
// TODO: handle exception
System.out.println("Exception at try catch block");
}
private void displayData(Cursor c2) {
// TODO Auto-generated method stub
if(c2 != null) // Should enter this statement. Always enters else condition
{
if(c2.getCount() > 0)
{
c2.moveToFirst();
do
{
// obtain data from cursor
}while(c2.moveToNext());
}
}
else
{
System.out.println("Cursor c2 is Empty");
}
}
投资组合管理器.java
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class PortfolioManager extends SQLiteOpenHelper{
static String name = "portfolio";
static int version = 1;
SQLiteDatabase sqlDB;
Cursor c;
public PortfolioManager(Context context) {
super(context, name, null, version);
// TODO Auto-generated constructor stub
sqlDB = getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase sqlDB) {
// TODO Auto-generated method stub
sqlDB.execSQL("create table if not exists maintable(scriptnumber text, scriptname text, totalquantity int, " +
"averageprice double, lossorgain double)");
}
@Override
public void onUpgrade(SQLiteDatabase sqlDB, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public Cursor getData(String string) {
// TODO Auto-generated method stub
String table = string;
sqlDB.rawQuery("select * from "+table, null);
return c;
}
public void addDataToTable(String scriptName, String dOP, int purchasedQuantity, double purchasedPrice,
double brokerage, double serviceTax, double stt, double stampDuty) {
// TODO Auto-generated method stub
sqlDB.execSQL("insert into "+scriptName+" values('"+dOP+"', '"+purchasedQuantity+"', '"+purchasedPrice+"'," +
"'"+brokerage+"', '"+serviceTax+"', '"+stt+"', '"+stampDuty+"')");
}
public void createTable(String scriptName) {
// TODO Auto-generated method stub
sqlDB.execSQL("create table if not exists "+scriptName+"(dateofpurchase text, purchasedquantity int, purchasedprice double," +
" brokerage double, servicetax double, stt double, stampduty double)");
}
}
输出 :::
01-17 23:38:16.799: I/System.out(23248): Creating table with name nal
01-17 23:38:16.869: I/System.out(23248): Table created
01-17 23:38:16.929: I/System.out(23248): data added
01-17 23:38:16.929: I/System.out(23248): Cursor c2 is Empty