从.sqlite获取包含多个包含多个列的表的列 [英] Getting a column from .sqlite containing multiple tables with multiple columns

查看:299
本文介绍了从.sqlite获取包含多个包含多个列的表的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

数据库由多个表和多个列组成,如图所示&我已经粘贴这个law.sqlite在assets文件夹。



数据库由多个表和多个列组成,如图所示&我已经在资源文件夹中粘贴了这个law.sqlite。



假设我想访问 AS_name 列的所有元素,如图所示。

解决方案

我试过,现在问题解决了:



面对类似问题的任何人都可以尝试我的实现:



步骤1:
$ b

创建一个GetterSetter类(在这里命名为 GS )生成所使用的变量的 Getter-Setters



在我的例子中:

  public class GS {
String AS_name;

public String getAS_name(){
return AS_name;
}

public void setAS_name(String aS_name){
AS_name = aS_name;
}
}

步骤2: / p>

创建一个 DBAdapter 类,扩展 SQLiteOpenHelper



剩下的只需要复制我的 DBAdapter.java code&注意实现方法 getData(),其中从数据库获取数据!

  public class DBAdapter extends SQLiteOpenHelper 
{
CustomAdapter adapter;
static String name =law.sqlite; // - 将它替换为sqlite名称
static String path =;
static ArrayList< GS> gs;
static SQLiteDatabase sdb;

@Override
public void onCreate(SQLiteDatabase db)
{
// TODO自动生成方法存根
}

@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
// TODO自动生成方法存根
}

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);
//刚刚创建的空路径db
String outFileName = path +/+ name;
//打开空数据库作为输出流
OutputStream myOutput = new FileOutputStream(outFileName);
//从输入文件传输字节到输出文件
byte [] buffer = new byte [1024]
int length;
while((length = myInput.read(buffer))> 0)
{
myOutput.write(buffer,0,length);
}
//关闭流
myOutput.flush();
myOutput.close();
myInput.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.setAS_name(c1.getString(3)); // ---这里3代表列号。
Log.v(AS_name,q1.AS_name +);
gs.add(q1);

}
}
catch(Exception e){
e.printStackTrace();
}
return gs;
}
}

步骤3: / p>

MainActivity.java

  public class MainActivity extends Activity {

ArrayList< GS> q = new ArrayList< GS>();
CustomAdapter适配器;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//从xml中获取ListView对象
lv =(ListView)findViewById(R.id.listView1);

DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
if(!db.checkDatabase())
{
db.createDatabase(getApplicationContext());
}
db.openDatabase();

q = db.getData();
for(int i = 0; i< q.size(); i ++)
{
Log.i(outside,+ q.get(i).getAS_name ));
}
lv =(ListView)findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(MainActivity.this,q));
//lv.setAdapter (adapter);

}

class CustomAdapter extends ArrayAdapter< GS>
{
ArrayList< GS>列表;
LayoutInflater mInfalter;
public CustomAdapter(Context context,ArrayList< GS> list)
{
super(context,R.layout.customlayout,list);
this.list = list;
mInfalter = LayoutInflater.from(context);
for(int i = 0; i {
Log.i(............... 。,+ list.get(i).getAS_name());
}

}
// public int getCount(){
// return list.size();
//}
@Override
public View getView(int position,View convertView,ViewGroup parent){
ViewHolder holder;
Log.i(..........,Hello in getView);
if(convertView == null)
{
convertView = mInfalter.inflate(R.layout.customlayout,parent,false); // - customlayout.xml必须有一个textView
holder = new ViewHolder();
holder.tv1 =(TextView)convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
} else {
holder =(ViewHolder)convertView.getTag();
}

holder.tv1.setText(list.get(position).getAS_name());
return convertView;
}

}
static class ViewHolder
{
TextView tv1;
}
}

运行此代码&最后在listview中的列表将显示! :)


!Database consists of multiple tables and multiple columns as shown & i have pasted this law.sqlite in assets folder.

Database consists of multiple tables and multiple columns as shown & i have pasted this law.sqlite in assets folder.

Suppose i want to access all the elements of column AS_name as shown . So how should i code for it?

解决方案

I tried and now the problem is solved :

For anyone facing similar type of problem can try my implementation :

Step 1:

Make a GetterSetter class (named GS here) & generate the Getter-Setters of variables used.

Like in my case:

public class GS {
     String AS_name;

public String getAS_name() {
return AS_name;
}

public void setAS_name(String aS_name) {
AS_name = aS_name;
}
}

Step 2:

Make a DBAdapter class which extends SQLiteOpenHelper & in that assign the your name of the file with extension .sqlite !

Rest you need only to copy my DBAdapter.java code & take care to implement the method getData() in which the data from database is fetched !

public class DBAdapter extends SQLiteOpenHelper
{
CustomAdapter adapter;
static String name = "law.sqlite"; //--Replace it with ur sqlite name 
static String path = "";
static ArrayList<GS> gs;
static SQLiteDatabase sdb;

@Override
public void onCreate(SQLiteDatabase db)
{
    // TODO Auto-generated method stub
}

@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();
    } 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.setAS_name(c1.getString(3)); //--- here 3 represents column no.
        Log.v("AS_name",q1.AS_name+""); 
        gs.add(q1);

    }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
    return gs;
}
}

Step 3:

The class MainActivity.java :

public class MainActivity extends Activity {

ArrayList<GS> q = new ArrayList<GS>();
CustomAdapter adapter;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
       // Get ListView object from xml
     lv = (ListView) findViewById(R.id.listView1);

    DBAdapter db = DBAdapter.getDBAdapter(getApplicationContext());
    if (!db.checkDatabase()) 
    {
        db.createDatabase(getApplicationContext());
    }
    db.openDatabase();

    q = db.getData();
    for(int i=0;i<q.size();i++)
    {
        Log.i("outside",""+q.get(i).getAS_name());
    }
    lv = (ListView) findViewById(R.id.listView1);
    lv.setAdapter(new CustomAdapter(MainActivity.this,q));
    //lv.setAdapter(adapter);

}

class CustomAdapter extends ArrayAdapter<GS>
  {
       ArrayList<GS> list;
       LayoutInflater mInfalter;    
       public CustomAdapter(Context context, ArrayList<GS> list)
       {
           super(context,R.layout.customlayout,list);
          this.list= list;  
          mInfalter = LayoutInflater.from(context);
          for(int i=0;i<list.size();i++)
         {
             Log.i("................",""+list.get(i).getAS_name());
         }

       }
//         public int getCount(){
//              return list.size();
//          }
       @Override
        public View getView(int position, View convertView, ViewGroup parent) {
          ViewHolder holder;
          Log.i("..........","Hello in getView");
          if(convertView==null)
          {
               convertView = mInfalter.inflate(R.layout.customlayout,parent,false);//--customlayout.xml must have a textView 
               holder = new ViewHolder();
               holder.tv1 = (TextView)convertView.findViewById(R.id.textView1); 
               convertView.setTag(holder); 
          }else{
                holder = (ViewHolder)convertView.getTag();
          } 

                holder.tv1.setText(list.get(position).getAS_name());
          return convertView;
    }

  }
 static class ViewHolder
    {
        TextView tv1;
    }  
}

Run this code & finally the list in the listview will be displayed ! :)

这篇关于从.sqlite获取包含多个包含多个列的表的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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