Android SQLite无法查询数据
来源:互联网 发布:腰包 知乎 编辑:程序博客网 时间:2024/05/22 00:40
Android SQLite无法查询数据,可以正常插入数据,也提示插入成功了,但是执行下列语句输入的英文字符,却总是会查出来数字,不是字符。。第一次接触这个,不清楚是怎么回事请帮忙说的详细点,谢谢。。
package com.example.path_n;
//主类
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.Random;
import org.w3c.dom.Text;
import junit.framework.Test;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity<person> extends Activity {
private DatebaseHelper dbHelper;
private TextView txtMsg;
//public EditText edi=new EditText(this);
private static final String DATABASE_NAME="mdb1.db";
private static final String TABEL_NAME="EMP_T";
private static final int DATABASE_VERSION=1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edi=new EditText(this);
//TextView txtMsg;
// STATR 建表及数据库
dbHelper = new DatebaseHelper(this, DATABASE_NAME, null,
DATABASE_VERSION);
txtMsg = (TextView) this.findViewById(R.id.textView1);
Button btn1 = (Button) this.findViewById(R.id.button1);
btn1.setText("初始化");
btn1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
CreateTable();
}}
);
//END
//statr 表操作
Button btn2 = (Button) this.findViewById(R.id.button2);
btn2.setText("插入 记录");
btn2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String str=edi.getText().toString();
if(!(str==""||str==null)){
insertItem(str);}else{txtMsg.setText(str+"is error");}
}
});
Button btn3 = (Button) this.findViewById(R.id.button3);
btn3.setText("显示数据");
btn3.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String str=edi.getText().toString();
if(str!=""||str!=null){
showItems(str);
}else{
txtMsg.setText(str+"is error");
}
}
});
Button btn4 = (Button) this.findViewById(R.id.button4);
btn4.setText("删除记录");
btn4.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
deleteItem();
}
});
//end
}
protected void deleteItem() {
// TODO Auto-generated method stub
}
protected void showItems(String str) {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getReadableDatabase();
String sql="name = "+"'"+str+"'";
/*try{
Cursor c = db.rawQuery("select * from emp_t where name=?",new String[]{str});
while(c.moveToNext()){
if(c.getCount()>0){
String name=c.getString(c.getColumnIndex("name"));
String data=c.getString(c.getColumnIndex("data"));
txtMsg.setText(name+","+data);
}else{
insertItem(str);
txtMsg.setText("ok");
}
}
//db.execSQL(sql);
}catch(SQLException ex){
txtMsg.setText("读取数据失败\n" + ex.toString() + "\n");
}*/
try {
String[] column = { "id", "name"};
Cursor cursor = db.query(TABEL_NAME, column, sql, null, null,
null, null);
Integer num = cursor.getCount();
txtMsg.append("共 " + Integer.toString(num) + " 条记录\n");
if(cursor!=null){
if(cursor.moveToFirst()){
do{
//int id = cursor.getColumnCount();
String name=cursor.getString(cursor.getColumnIndexOrThrow("NAME"));
txtMsg.setText(name);
}while(cursor.moveToNext());
}
}
/*while (cursor.getPosition() != cursor.getCount()) {
txtMsg.setText(Integer.toString(cursor.getPosition()) + ","
+ cursor.getString(0) + ","
+ cursor.getColumnIndex("name") + "\n");
cursor.moveToNext();
}*/
} catch (SQLException ex) {
txtMsg.setText("读取数据失败\n" + ex.toString() + "\n");
}
}
protected void insertItem(String text) {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getWritableDatabase();
//String str=edi.getText().toString();
//if(str==""||str==null){str="TEST1";}
try {
// _execSQL() - 执行指定的 sql
String sql="insert into "+TABEL_NAME+" (name) values ('"+text+"')";
db.execSQL(sql);
txtMsg.setText("成功插入 数据\n");
} catch (SQLException ex) {
txtMsg.setText("插入数据失败\n" + ex.toString() + "\n");
}
}
/*Random random = new Random();
for (int i = 0; i < 3; i++) {
String sql = "insert into " + TABEL_NAME
+ " (name, age) values ('name" + String.valueOf(i)
+ "', " + random.nextInt() + ")";*/
protected void CreateTable() {
// TODO Auto-generated method stub
SQLiteDatabase db = dbHelper.getWritableDatabase();
String sql = "CREATE TABLE IF NOT EXISTS " + TABEL_NAME
+ " (ID INTEGER PRIMARY KEY, Name TEXT);";
try {
db.execSQL(sql);
txtMsg.setText("数据表成功创建\n");
} catch (SQLException ex) {
txtMsg.setText("数据表创建错误\n" + ex.toString() + "\n");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
//DB类
package com.example.path_n;
import android.content.Context;
//import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class DatebaseHelper extends SQLiteOpenHelper {
public DatebaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
}
public void onOpen(SQLiteDatabase db){
super.onOpen(db);
}
}
0 0
- Android SQLite无法查询数据
- Android:SQLite无法update/insert/delete数据(数据库被locked)
- android SQLite 优化(四)数据查询 (待续...)
- android中的sqlite查询数据去掉重复值
- android中Sqlite查询的数据返回排序问题
- Android sqlite模糊查询
- android sqlite查询方式
- android中sqlite查询
- sqlite 查询一条重复数据
- Android SQLite 数据操作
- android sqlite数据操作
- Android SQLite数据操作
- android sqlite 数据初始化
- android数据存储--SQLite
- Android数据存储---SQLite
- Android数据存储-SQLite
- android-数据存储sqlite
- Android---数据存储SQLite
- vim自动补全神器Youcompleteme的安装
- 系统学习C语言
- 系统功能设置
- 安卓定时任务Timer一直失败
- androidStudio运行项目报错,
- Android SQLite无法查询数据
- 有没有方法能够自动获取手机的“有权查看使用情况”这个权限
- ASP编写一个网页界面,页面中设置按钮,按钮能够调用外部命令行程序
- JAVA 数组转换成List及List转换成数组
- 分解质因数
- IT人员应该经常逛的网站
- 大小堆的实现与实现优先级队列
- 杂记一篇,写给16年
- 利用gpu加速神经网络算法