在SQLite中获得一个独特的行

时间:2014-07-05 07:31:44

标签: android android-sqlite

我正在尝试使用String,&来搜索sqlite中的项目。试图返回该行中包含的描述。项目存储在名为Articles的表中,列名为A_name,描述列名称为AS_name

这是我的代码,游标不是null,但是while循环没有执行一次

public String searchData(String text)
{
    Cursor cursor = sdb.query("Articles", new String[] {"A_name","AS_name"}, " A_name=?",new String[]{text}, null, null, null);

     Log.e("running", "cursor run");


     String temp = null,temp2 = null;
     if(cursor!=null)
     {

         Log.e("running", "curosr is not null");
     while(cursor.moveToFirst())
     {

         Log.e("running", "curosr while loop enter");

          temp =  (cursor.getString(cursor.getColumnIndex("A_name")));
          //temp2 =(cursor.getString(cursor.getColumnIndex("AS_name")));
        Log.e("running", "id  email" +temp+ " name"+temp2);

    }
 }
     return temp;
}

我想返回AS_name的相应元素,我也很困惑我应该在while循环中写什么?

任何人都可以说出我的错误,提前谢谢......

更新 DBAdapter.java

public class DBAdapter extends SQLiteOpenHelper
{
//CustomAdapter adapter;
static String name = "law6.sqlite";
static String path = "";
static ArrayList<GS> gs;
static SQLiteDatabase sdb;

@Override
public void onCreate(SQLiteDatabase db)
{
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
    // TODO Auto-generated method stub
} 

private DBAdapter(Context v) 
{
    super(v, name, null, 1);
    path = "/data/data/" + v.getApplicationContext().getPackageName() + "/databases";
}

public boolean checkDatabase()
{
    SQLiteDatabase db = null;
    try 
    {
        db = SQLiteDatabase.openDatabase(path + "/" + name, null, SQLiteDatabase.OPEN_READONLY);
    } catch (Exception e) 
    {
        e.printStackTrace();
    }
    if (db == null) 
    {
        return false;
    } 
    else
    {
        db.close();
        return true;
    }
}

public static synchronized DBAdapter getDBAdapter(Context v)
{
    return (new DBAdapter(v));
}

public void createDatabase(Context v) 
{
    this.getReadableDatabase();
    try
    {
        InputStream myInput = v.getAssets().open(name);
        // Path to the just created empty db
    String outFileName = path +"/"+ name;
        // Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);
        // transfer bytes from the inputfile to the outputfile
    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) 
    {
        myOutput.write(buffer, 0, length);
    }
        // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

    /*  



        InputStream is = v.getAssets().open("quiz.sqlite");
        // System.out.println(is.available());
        System.out.println(new File(path + "/" + name).getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(path + "/" + name);
        int num = 0;
        while ((num = is.read()) > 0) {
            fos.write((byte) num);
        }
        fos.close();
        is.close();*/
    } catch (IOException e) 
    {
        System.out.println(e);
    }
}

public void openDatabase() 
{
    try 
    {
        sdb = SQLiteDatabase.openDatabase(path + "/" + name, null,
                SQLiteDatabase.OPEN_READWRITE);
    } catch (Exception e) 
    {
        System.out.println(e);
    }
}

public ArrayList<GS> getData() 
{
    try{
    Cursor c1 = sdb.rawQuery("SELECT DISTINCT * FROM Articles", null);
    gs = new ArrayList<GS>();
    while (c1.moveToNext())
    {
        GS q1 = new GS();
        q1.setId(c1.getString(0));
        q1.setA_id(c1.getString(1));
        q1.setA_name(c1.getString(2));
        q1.setAS_name(c1.getString(3));
        q1.setDesc_art(c1.getString(4));
    //  q1.setAct(c1.getString(5));
        q1.setExtra(c1.getString(6));
        q1.setPart(c1.getString(7));
        q1.setItalic(c1.getString(8));
        Log.v("AS_name",q1.AS_name);
        gs.add(q1);
    }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return gs;
}

public String searchData(String text)
{
    Cursor cursor = sdb.query("Articles", new String[] {"A_name","AS_name"}, " A_name=?",new String[]{text}, null, null, null);

     Log.e("running", "cursor run");


     String temp = null,temp2 = null;
     if(cursor!=null)
     {

         Log.e("running", "curosr is not null");
        Log.v("", ""+cursor.getCount());
     while(cursor.moveToNext())
     {

         Log.e("running", "curosr while loop enter");

          temp =  (cursor.getString(cursor.getColumnIndex("AS_name")));
         // temp2 =(cursor.getString(cursor.getColumnIndex(name)));
        Log.e("running", "desc" +temp);

    }
 }
     return temp;
}
}

在实施rajaji回答后更新,我收到此错误: enter image description here

2 个答案:

答案 0 :(得分:0)

你的Uri是否正确?

创建Uri取决于您的包名和表名,如下面的代码:

    private static final String AUTHORITY           = "com.sample.test_db_provider";
    private static final String BASE_PATH           = "Tables";
    private static final Uri    Sample_URI  = Uri.parse("content://" + AUTHORITY
                                                        + "/" + BASE_PATH
                                                        + "/SampleTable");

然后在你的get方法中,使用Uri

查询db,如下面的代码
cur = m_Resolver.query(Sample_URI, new String[] {"A_name","AS_name"},
                    "SampleTable.A_name = " + text, null, null);
            if(cur != null && cur.moveToFirst())
            {
                do
                {
                    ...
                }
                while(cur.moveToNext());
            }
            cur.close();

我使用inline where子句,但您可以使用参数执行此操作。这是你想要的?

答案 1 :(得分:0)

试试这会对你有所帮助

创建获取数据的原始查询

public String searchData(String text)
{
String strvalue=null;
SQliteDatabase db=this.getwritabledatabase();
Cursor cur=null;
String strquery="select * from youurtablename where A_name="+text;
cur=db.rawQuery(strquery,null); 
if(cur!=null&&cur.moveToFirst())
{
do
            {
                strvalue=cur.getString(0);
            }
            while(cur.moveToNext());
}  
}

一旦完成,请告知我们