如何从android中的查询方法中检索数据 [英] how to retrieve data from query method in android
本文介绍了如何从android中的查询方法中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好朋友
我是android新手。我正在尝试创建一个简单的数据库应用程序。但是当我尝试从数据库中检索超过1个数据项时,我遇到运行时问题。
我的代码在这里---
public class DataHelper extends SQLiteOpenHelper {
public DataHelper(Context c){
super (c, mydb,null, 1 跨度>);
}
public void onCreate(SQLiteDatabase db){
db .execSQL( create table records(id integer primary key autoincrement,name text not null,address text,age integer) 跨度>);
}
public void onUpgrade(SQLiteDatabase db, int oldv, int newv){
db.execSQL( drop table如果存在记录);
this .onCreate(db);
}
public void insertdata( String name, String address, int age){
SQLiteDatabase sd = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put( name,name);
cv.put( address,address);
cv.put( age,年龄);
sd.insert( records,null,cv);
}
public String getdata(){
SQLiteDatabase sd = this.getWritableDatabase();
String result = null;
String fname = null;
String city = null;
int age;
Cursor c = sd.query( records,null,null,null,null ,null,null);
if (c.moveToFirst()){
do {
fname = c.getString(c.getColumnIndex( name));
city = c.getString(c.getColumnIndex( address));
age = c.getInt(c.getColumnIndex( age));
result = fname + / n + city + / n + age;
} while (c.moveToNext());
}
返回结果;
}
}
活动类代码在这里----
public class Act5 extends 活动{
DataHelper help;
SQLiteDatabase sd;
EditText e1,e2,e3;
TextView t;
按钮b1,b2;
public void onCreate(Bundle b){
超跨度> .onCreate(b);
setContentView(R.layout.main);
help = new DataHelper( this );
e1 =(EditText)findViewById(R.id.et1);
e2 =(EditText)findViewById(R.id.et2);
e3 =(EditText)findViewById(R.id.et3);
b1 =(Button)findViewById(R.id.bt1);
b2 =(Button)findViewById(R.id.bt2);
t =(TextView)findViewById(R.id.tv);
b1.setOnClickListener( new OnClickListener(){
@覆盖
public void onClick(查看v){
< span class =code-comment> // TODO自动生成的方法存根
String name = e1.getText()。toString();
String city = e2.getText()。toString();
字符串 ages = e3.getText()。toString();
int age = Integer .parseInt(years);
sd = help.getWritableDatabase();
help.insertdata(name,city,age);
e1.setText( );
e2.setText( );
Toast.makeText(getApplicationContext(), 您保存的数据,Toast.LENGTH_LONG) 。节目();
}
});
b2.setOnClickListener( new OnClickListener(){
@ Override
public void onClick(查看v){
// TODO自动生成的方法存根
sd = help.getWritableDatabase();
字符串 result = help.getdata();
t.setText( 您的数据是: +结果);
}
});
}
}
解决方案
查看此链接。
basic-databse-operations- in-sqlite [ ^ ]
hello friends
i am new to android. i am trying to create a simple database application.but i getting run time problem when i try to retrive more then 1 data item from database.
my code is here---
public class DataHelper extends SQLiteOpenHelper{
public DataHelper(Context c){
super(c,"mydb",null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("create table records(id integer primary key autoincrement,name text not null,address text,age integer)");
}
public void onUpgrade(SQLiteDatabase db,int oldv,int newv){
db.execSQL("drop table if exist records");
this.onCreate(db);
}
public void insertdata(String name,String address,int age){
SQLiteDatabase sd=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put("name", name);
cv.put("address",address);
cv.put("age",age);
sd.insert("records", null, cv);
}
public String getdata(){
SQLiteDatabase sd=this.getWritableDatabase();
String result=null;
String fname=null;
String city=null;
int age;
Cursor c=sd.query("records", null, null, null, null, null, null);
if(c.moveToFirst()){
do{
fname=c.getString(c.getColumnIndex("name"));
city=c.getString(c.getColumnIndex("address"));
age=c.getInt(c.getColumnIndex("age"));
result=fname+"/n"+city+"/n"+age;
}while(c.moveToNext());
}
return result;
}
}
Activity class code is here----
public class Act5 extends Activity {
DataHelper help;
SQLiteDatabase sd;
EditText e1,e2,e3;
TextView t;
Button b1,b2;
public void onCreate(Bundle b){
super.onCreate(b);
setContentView(R.layout.main);
help=new DataHelper(this);
e1=(EditText)findViewById(R.id.et1);
e2=(EditText)findViewById(R.id.et2);
e3=(EditText)findViewById(R.id.et3);
b1=(Button)findViewById(R.id.bt1);
b2=(Button)findViewById(R.id.bt2);
t=(TextView)findViewById(R.id.tv);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String name=e1.getText().toString();
String city=e2.getText().toString();
String ages=e3.getText().toString();
int age=Integer.parseInt(ages);
sd=help.getWritableDatabase();
help.insertdata(name,city,age);
e1.setText("");
e2.setText("");
Toast.makeText(getApplicationContext(), "your data saved", Toast.LENGTH_LONG).show();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sd=help.getWritableDatabase();
String result=help.getdata();
t.setText("your data is: "+result);
}
});
}
}
解决方案
check this link .
basic-databse-operations-in-sqlite[^]
这篇关于如何从android中的查询方法中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文