嗨,大家好,我需要你的帮助.我在我的应用程序中创建sqlite数据库,然后将数据插入其中.现在我想从中检索数据,但我只想插入一个数据并检索它然后将其显示到TextView中.
public class Db_sqlit extends sqliteOpenHelper{
String TABLE_NAME = "BallsTable";
public final static String name = "db_data";
public Db_sqlit(Context context) {
super(context, name, null, 1);
}
@Override
public void onCreate(sqliteDatabase db) {
db.execsql("create table "+TABLE_NAME+" (id INTEGER PRIMARY KEY AUTOINCREMENT, ball TEXT)");
}
@Override
public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {
db.execsql("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(db);
}
public boolean insertData(String balls){
sqliteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("ball",balls);
long result = db.insert(TABLE_NAME,null,contentValues);
if(result == -1){
return false;
}
else
return true;
}
public void list_balls(TextView textView) {
Cursor res = this.getReadableDatabase().rawQuery("select ball from "+TABLE_NAME+"",null);
textView.setText("");
while (res.movetoNext()){
textView.append(res.getString(1));
}
}
}
解决方法:
这是我如何实现这一目标的一个例子.
首先创建一个类,我打电话给我
DBManager.java
public class DBManager {
private Context context;
private sqliteDatabase database;
private sqliteHelper dbHelper;
public DBManager(Context c) {
this.context = c;
}
public DBManager open() throws sqlException {
this.dbHelper = new sqliteHelper(this.context);
this.database = this.dbHelper.getWritableDatabase();
return this;
}
public void close() {
this.dbHelper.close();
}
public void insert(String name, String desc) {
ContentValues contentValue = new ContentValues();
contentValue.put(sqliteHelper.NAME, name);
contentValue.put(sqliteHelper.AGE, desc);
this.database.insert(sqliteHelper.TABLE_NAME_STUDENT, null, contentValue);
}
public Cursor fetch() {
Cursor cursor = this.database.query(sqliteHelper.TABLE_NAME_STUDENT, new String[]{sqliteHelper._ID, sqliteHelper.NAME, sqliteHelper.AGE}, null, null, null, null, null);
if (cursor != null) {
cursor.movetoFirst();
}
return cursor;
}
public int update(long _id, String name, String desc) {
ContentValues contentValues = new ContentValues();
contentValues.put(sqliteHelper.NAME, name);
contentValues.put(sqliteHelper.AGE, desc);
return this.database.update(sqliteHelper.TABLE_NAME_STUDENT, contentValues, "_id = " + _id, null);
}
public void delete(long _id) {
this.database.delete(sqliteHelper.TABLE_NAME_STUDENT, "_id=" + _id, null);
}
}
sqliteHelper.java
public class sqliteHelper extends sqliteOpenHelper {
public static final String AGE = "age";
private static final String CREATE_TABLE_STUDENT = " create table STUDENTS ( _id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL , age TEXT );";
private static final String DB_NAME = "STUDENTS.DB";
private static final int DB_VERSION = 1;
public static final String NAME = "name";
public static final String TABLE_NAME_STUDENT = "STUDENTS";
public static final String _ID = "_id";
public sqliteHelper(Context context) {
super(context, DB_NAME, null, 1);
}
public void onCreate(sqliteDatabase db) {
db.execsql(CREATE_TABLE_STUDENT);
}
public void onUpgrade(sqliteDatabase db, int oldVersion, int newVersion) {
db.execsql("DROP TABLE IF EXISTS STUDENTS");
onCreate(db);
}
}
加上:
在这个例子中,我从EditText中获取文本,当单击该按钮时,我检查EditText是否为空.如果它不是空的并且学生尚不存在,我将学生姓名和年龄插入数据库.我显示Toast,让用户知道状态:
btnAdd.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (edtName.getText().toString().trim().length() == 0) {
Toast.makeText(getApplicationContext(), "Please provide your students name", Toast.LENGTH_SHORT).show();
} else{
try {
if (edtAge.getText().toString().trim().length() != 0) {
String name = edtName.getText().toString().trim();
String age = edtAge.getText().toString().trim();
String query = "Select * From STUDENTS where name = '"+name+"'";
if(dbManager.fetch().getCount()>0){
Toast.makeText(getApplicationContext(), "Already Exist!", Toast.LENGTH_SHORT).show();
}else{
dbManager.insert(name, age);
Toast.makeText(getApplicationContext(), "Added successfully!", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getApplicationContext(), "please provide student age!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
e.printstacktrace();
}
}
}
});
更新:
在这里,我在EditText中使用Text,并在单击按钮时更新学生.您还可以将以下内容放在try / catch中以确保它已成功更新.
btnupdate.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = nameText.getText().toString();
String age = ageText.getText().toString();
dbManager.update(_id, name, age);
Toast.makeText(getApplicationContext(), "Updated successfully!", Toast.LENGTH_SHORT).show();
}
});
删除:
dbManager.delete(_id);
Toast.makeText(getApplicationContext(), "Deleted successfully!", Toast.LENGTH_SHORT).show();
要得到:
DBManager dbManager = new DBManager(getActivity());
dbManager.open();
Cursor cursor = dbManager.fetch();
cursor.movetoFirst();
final TextView studentName = (TextView) getActivity().findViewById(R.id.nameOfStudent);
studentName.settext(cursor.getString(0));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。