在sq lite中传递字符串变量,android程序 [英] Passing string variable in sq lite,android programs
本文介绍了在sq lite中传递字符串变量,android程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hai,
如何解决此错误。我是Android应用程序的新手。请帮忙。
我的代码就像是
Hai,
How can i solve this error. I'm new in android apps. please help.
my code is like
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
public static final String KEY_ROWID = "_id";
public static final String KEY_USERNAME = "username";
public static final String KEY_PWDL = "pwdl";
public static final String KEY_PWDD = "pwdd";
public static final String KEY_ROLE = "role";
private static final String TAG = "DBAdap";
private static final String DATABASE_NAME = "Paydb";
private static final String DATABASE_TABLE = "table1";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table table1 (_id integer primary key, " + "username text not null,"+ " pwdl text not null, "
+ "pwdd text not null,"+ "role integer not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS table1");
onCreate(db);
}
}
// ---opens the database---
public DBAdapter open() throws SQLException
{
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close()
{
DBHelper.close();
}
// ---insert a table1into the database---
public long insertUser(Integer id, String name, String pwdl,String pwdd,Integer role)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ROWID, id);
initialValues.put(KEY_USERNAME, name);
initialValues.put(KEY_PWDL, pwdl);
initialValues.put(KEY_PWDD, pwdd);
initialValues.put(KEY_ROLE, role);
return db.insert(DATABASE_TABLE, null, initialValues);
}
// ---retrieves a particular table1---
//In here I'm passing an integer variable and this function working smoothly
public Cursor getUser1(Integer Role) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PWDL, KEY_PWDD,KEY_ROLE }, KEY_ROLE +"="+ Role, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
//in here I'm passing a string variable and this function find the error.
public Cursor getUser2(String user) throws SQLException
{
Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME, KEY_PWDL, KEY_PWDD,KEY_ROLE }, KEY_USERNAME +"=" + user, null, null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}
问候
vani
regards
vani
推荐答案
尝试类似:
Try something like:
public Cursor getUser2(String user) throws SQLException
Cursor cursor = null;
String[] columns = null; // select all columns
String selection = KEY_USERNAME + "=?";
String[] selectionArgs = new String[1];
selectionArgs[0] = user;
String groupBy = null;
String having = null;
String orderBy = null;
cursor = db.query(DATABASE_TABLE,
columns,
selection,
selectionArgs,
groupBy,
having,
orderBy
);
return cursor;
}
这篇关于在sq lite中传递字符串变量,android程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文