我正在编写一个从用户获取名字和姓氏并将其插入数据库的应用程序,然后用户可以在另一个字段中写入第一个名称并搜索姓氏,程序应该显示TextView中搜索到的名称的姓氏。
package ir.itstuff.dbtest;
import java.util.List;
import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText fName = (EditText) findViewById(R.id.editText1);
final EditText sName = (EditText) findViewById(R.id.editText2);
final TextView tv = (TextView) findViewById(R.id.textView1);
EditText et3 = (EditText) findViewById(R.id.editText3);
Button btdt = (Button) findViewById(R.id.button2);
Button save = (Button) findViewById(R.id.button1);
final SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE,null);
db.execSQL("CREATE TABLE IF NOT EXISTS MyTable(id INTEGER PRIMARY KEY AUTOINCREMENT, FirstName varchar,SecondName varchar)");
final String first_name = fName.getText().toString();
final String second_name = sName.getText().toString();
final String search_name = et3.getText().toString();
// Inserting data from inputs
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
db.execSQL("INSERT INTO MyTable (FirstName,SecondName) VALUES ('"
+ first_name + "','" + second_name + "')");
Toast.makeText(getBaseContext(), "Data Inserted",
Toast.LENGTH_LONG).show();
db.close();
}
});
btdt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// db.execSQL("SELECT * FROM MyTable WHERE FirstName='"+search_name+"'");
Cursor c;
c = db.query(false, "MyTable",null, "FirstName="+search_name, null, null, null, null, null);
while(c.moveToNext()){
String result = c.getString(c.getColumnIndex("LastName"));
tv.setText(result);
}
db.close();
}
});
}
@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;
}
}
直到程序将数据插入数据库才能正常工作,但是当我搜索名称时,程序崩溃。 这是log cat输出:
04-30 08:55:14.931: E/AndroidRuntime(1848): FATAL EXCEPTION: main
04-30 08:55:14.931: E/AndroidRuntime(1848): Process: ir.itstuff.dbtest, PID: 1848
04-30 08:55:14.931: E/AndroidRuntime(1848): android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: SELECT * FROM MyTable WHERE FirstName=
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1161)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1032)
04-30 08:55:14.931: E/AndroidRuntime(1848): at ir.itstuff.dbtest.Main$2.onClick(Main.java:60)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.view.View.performClick(View.java:4438)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.view.View$PerformClick.run(View.java:18422)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.os.Handler.handleCallback(Handler.java:733)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.os.Handler.dispatchMessage(Handler.java:95)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.os.Looper.loop(Looper.java:136)
04-30 08:55:14.931: E/AndroidRuntime(1848): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-30 08:55:14.931: E/AndroidRuntime(1848): at java.lang.reflect.Method.invokeNative(Native Method)
04-30 08:55:14.931: E/AndroidRuntime(1848): at java.lang.reflect.Method.invoke(Method.java:515)
04-30 08:55:14.931: E/AndroidRuntime(1848): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-30 08:55:14.931: E/AndroidRuntime(1848): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-30 08:55:14.931: E/AndroidRuntime(1848): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:3)
您正在关闭数据库,然后尝试再次使用它而不打开它。当有人点击btdt
时,数据库将关闭。在使用名字查询之前,您需要再次打开它。
答案 1 :(得分:0)
根据您的编辑,您的问题似乎出现在查询中:
在查询中使用?
,android将解析值。
类似的东西:
final String selectQuery = "SELECT " + BACKUP_TABLE_NAME + ".*, " + BACKUPCONTACTS_TABLE_NAME + "." + BACKUPCONTACTS_ID + ", "
+ BACKUPCONTACTS_TABLE_NAME + "." + BACKUPCONTACTS_VCARD + ", " + BACKUPCONTACTS_TABLE_NAME + "." + BACKUPCONTACTS_DISPLAY_NAME
+ " FROM " + BACKUP_TABLE_NAME + ", " + BACKUPCONTACTS_TABLE_NAME
+ " WHERE " + BACKUP_TABLE_NAME + "." + BACKUP_ID + " = " + BACKUPCONTACTS_TABLE_NAME+ "." + BACKUPCONTACTS_ID_BACKUP_FK
+ " AND " + BACKUP_TABLE_NAME + "." + BACKUP_ID + " = ?";
final Cursor cursor = db.rawQuery(selectQuery, new String[]{String.valueOf(id)});
所以,在你的情况下,它将是:
String query = "SELECT * FROM MyTable WHERE FirstName " + " = ? "
Cursor cursor = db.rawQuery(selectQuery, new String[]{search_name});
提示:使用驼峰大小写命名变量,而不是下划线。这是java惯例。因此,search_name
将重命名为searchName
答案 2 :(得分:-1)
首先,您的create table查询不正确。因为SQLite不支持varchar数据类型。而不是varchar使用TEXT数据类型然后,
尝试使用此语法而不是语法来从表记录中获取光标
c = db.query("MyTable",null,"FirstName=?",new String[]{FirstName }, null, null, null, null);