0

在使用创建表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
4

2 回答 2

1

一个问题是您需要使用由rawQuery(). 改变这个:

public Cursor getData(String string) {
    // TODO Auto-generated method stub

    String table = string;

    sqlDB.rawQuery("select * from "+table, null);       
    return c;
}

类似于:

public Cursor getData(String table) {
    return sqlDB.rawQuery("select * from "+table, null);       
}

此外,您的 if-else 块中的逻辑也不完全正确。除非rawQuery()抛出异常,否则它将始终返回游标,因此它可能为,但可能不会为空。尝试:

if(c2 != null) // Should enter this statement. Always enters else condition
{                   
    if(c2.getCount() > 0)
    {
        while(c2.moveToNext());
            // read data
        }
    }
    else
    {
        System.out.println("Cursor c2 is Empty");
    }
}
else
{
    System.out.println("Somethings gone horribly wrong");
}
于 2013-01-17T18:29:44.607 回答
0

您不是在此方法中分配光标的值getData(String string);

我认为你的getData(String string)方法应该是这样的,而不是旧的:

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;
    }
于 2013-01-17T18:30:54.380 回答