[英]Android SQLite Database check if query exists?
Hi i'm still new in android and SQLite. 嗨,我仍然是Android和SQLite的新手。 I got activity which that can add query to my attached db file. 我得到的活动可以向附加的db文件中添加查询。 The problem is, i can't add data using my methods. 问题是,我无法使用我的方法添加数据。 Is there a simple methods to check if query is exists ? 有没有简单的方法来检查查询是否存在?
Here is my DB Access 这是我的数据库访问权限
public class DBAccess {
private SQLiteOpenHelper openHelper;
private SQLiteDatabase db;
private static DBAccess instance;
Cursor c = null;
private DBAccess(Context context)
{
this.openHelper = new DatabaseOpenHelper(context);
}
public static DBAccess getInstance(Context context)
{
if(instance==null)
{
instance=new DBAccess(context);
}
return instance;
}
public void open()
{
this.db = openHelper.getWritableDatabase();
}
public void close()
{
if(db!=null)
{
this.db.close();
}
}
public void tambah(String a,String b)
{
String query= ("insert into TabelAbjad (kata,arti) values('"+a+"','"+b+"')");
db.execSQL(query);
}
public boolean checkdata(String c, String d)
{
String s;
String query= ("select kata from TabelAbjad where kata = '"+c+"' AND kata = '"+d+"'");
db.execSQL(query);
return true;
}
Here is when i try to call the methods 这是当我尝试调用方法时
private void adddata()
{
DBAccess dbAccess = DBAccess.getInstance(getApplicationContext());
dbAccess.open();
String k = editkata.getText().toString().trim();
String a = editarti.getText().toString().trim();
if (dbAccess.checkdata(k,a))
{
Toast.makeText(this, "Data already exists",Toast.LENGTH_LONG).show();
dbAccess.close();
finish();
}
else
{
dbAccess.tambah(k,a);
dbAccess.close();
Toast.makeText(this, "Data Saved",Toast.LENGTH_LONG).show();
}
}
PS : I'm call the method in button PS:我在按钮中调用方法
You are missing a semicolon (;) in your query string. 您在查询字符串中缺少分号(;)。 Try this String query= ("select kata from TabelAbjad where kata = '"+c+"' AND kata = '"+d+"';");
试试这个String query= ("select kata from TabelAbjad where kata = '"+c+"' AND kata = '"+d+"';");
for all your query strings 对于所有查询字符串
Your issue is that no row in the table will exist where the column kata has the value c as well as ( AND
) the value d it is impossible as a column can only have a single value, thus no rows would ever be extracted. 您的问题是,表中的行将不存在,其中列kata的值c以及( AND
)值d都是不可能的,因为列只能有一个值,因此将不会提取任何行。
Perhaps you want to find rows that have either c OR
d 也许您想查找具有c OR
d的行
in which case you could use :- 在这种情况下,您可以使用:-
String query= ("select kata from TabelAbjad where kata = '"+c+"' OR kata = '"+d+"'");
AND
has been changed to OR
即AND
已更改为OR
Furthermore, execSQL
cannot return a result, as per :- 此外, execSQL
无法返回结果,如:-
Execute a single SQL statement that is NOT a SELECT or any other SQL statement that returns data. 执行一个不是SELECT的SQL语句或任何其他返回数据的SQL语句。 execSQL execSQL
You thus need to either use the rawQuery
method or the convenience query
method, the latter recommended unless it's limitations mean that it cannot be used. 因此,您需要使用rawQuery
方法或便捷query
方法,建议使用后者,除非它的局限性意味着无法使用它。 As such you should try using something along the lines of (OR instead of AND assumed) :- 因此,您应该尝试使用类似(OR或AND假设)的东西:
public boolean checkdata(String c, String d)
boolean rv = false;
String whereargs = "kata=? OR kata=?"; // the WHERE clause less the WHERE keyword ? for the arguments (used on a 1 for 1 basis)
String[] whereargs = new String[]{c,d}; // The arguments that will be substituted into the SQL
String[] columns = new String[]{"kata"};
Cursor csr = db.query("TabelAbjad",columns,whereclause,whereargs,null,null,null);
if (csr.getCount() > 0) {
rv = true;
}
csr.close();
return rv;
}
getCount()
is used (it returns the number of rows extracted which could be 0 if none)). 您将数据(行)检索到游标中(在这种情况下,您只想知道是否有任何行符合给定条件,因此使用了getCount()
(它返回提取的行数,如果没有则返回0))。 Is there a simple methods to check if query is exists ? 有没有简单的方法来检查查询是否存在?
I believe that what you really mean is there anyway to check if the query returned any results. 我相信您真正的意思是无论如何都要检查查询是否返回了任何结果。
As said above a query returns data via a Cursor (like a table but according to the query eg the Cursor above will consist of a number of rows (0 or more) with a single column named kata (ie the query is SELECT **kata** FROM .....
) ) and thus you need to use a suitable method. 如上所述, 查询通过游标 (如表,但根据查询返回数据),例如,上面的游标将由多行(0或更多)组成,并且具有名为kata的单列(即查询为SELECT **kata** FROM .....
)),因此您需要使用合适的方法。
You can check/access numerous aspects/properties. 您可以检查/访问众多方面/属性。 Typically you'd move around the Cursor (eg while (your_cursor.moveToNext) {.... do things ....}
can be used traverse all rows in the cursor). 通常,您会在光标周围移动 (例如, while (your_cursor.moveToNext) {.... do things ....}
可用于遍历光标中的所有行时)。 Cursor . 游标 。
Once positioned appropriately at a row then you can use the get????(column_offset)
to retrieve the data that came from the database (where column_offset is an integer with 0 representing the first column, however it is generally much wiser to retrieve the actual offset using the getColumnIndex(column_name_as_a_string method.)) 一旦适当地定位在行上,则可以使用get????(column_offset)
检索来自数据库的数据(其中column_offset是一个整数,其中0表示第一列,但是通常更明智的做法是检索使用getColumnIndex(column_name_as_a_string方法。)的实际偏移量
So assuming that you wanted the data (String) in the first row that was extracted above into the Cursor csr then you could use :- 因此,假设您想将第一行中的数据(字符串)提取到游标csr中,则可以使用:-
if (csr.moveToFirst()) {
String mykata = csr.getString(csr.getColumnIndex("kata"));
}
csr.close(); // You should always close a cursor when done with it.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.