After creating the table with pm.createTable()
and adding data to it using pm.addDataToTable()
, I am trying to retrieve the data using pm.getData()
of the same table, that i just created and inserted, and store it in cursor c. I am not getting any compilation errors or exceptions. The control always enters the else condition in displayData()
. Can someone help me with this?
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");
}
}
portfoliomanager.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)");
}
}
Output :::
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
You are not Assign value of cursor in this method getData(String string);
I think your getData(String string)
method should be like this instead of old one:
public Cursor getData(String string) {
// TODO Auto-generated method stub
Curser c= null;
String table = string;
c = sqlDB.rawQuery("select * from "+table, null);
return c;
}