获取SQLite中唯一行 [英] Getting a unique row in SQLite

查看:164
本文介绍了获取SQLite中唯一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用字符串,和放大器来搜索sqlite的一个项目;试图返回包含该行中的描述。项目被存储在一个名为文章与列名 A_name ,说明列名 AS_name表

I am trying to search an item in sqlite by using String, & trying to return description contained in that row. items are stored in the table named Articles with column name A_name, Description column name is AS_name

这是我的code,光标不为空,但没有得到执行一次while循环

This is my code, the cursor is not null, but the while loop is not getting executed once

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循环?

I want to return the corresponding element of AS_name, also I am confused what should I wrote in while loop ?

任何人都可以请指出我的错误,在此先感谢...

Can anyone please identify my mistake, Thanks in advance...

更新 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答案后

更新,我得到这个错误:

UPDATE after implementing rajaji answer, I got this error :

推荐答案

试试这个它会帮助你。

创建原始查询获取数据

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());
}  
}

让我通知你完成

这篇关于获取SQLite中唯一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆