我正在尝试将我从EditText
获取的值插入到Mysql
数据库中,但我在"附近有错误,"我希望有人能澄清一下:
final SQLiteDatabase userDB = this.openOrCreateDatabase("Users",MODE_PRIVATE,null);
final String fullNameText = fullName.getText().toString();
final String stateText = state.getText().toString();
final String zipcodeText = zipcode.getText().toString();
saveUserInfo.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
userDB.execSQL("CREATE TABLE IF NOT EXISTS allUsers (name VARCHAR, zipcode VARCHAR, state VARCHAR, id INTEGER PRIMARY KEY)");
userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES ( " + fullNameText + ", " + zipcodeText + ", " + stateText + ")");
Cursor c = userDB.rawQuery("SELECT * FROM allUsers", null);
int nameIndex = c.getColumnIndex("name");
int zipcodeIndex = c.getColumnIndex("zipcode");
int stateIndex = c.getColumnIndex("state");
int idIndex = c.getColumnIndex("id");
c.moveToFirst();
while (c != null) {
Log.i("Name-", c.getString(nameIndex));
Log.i("Zipcode-", c.getString(zipcodeIndex));
Log.i("State-", c.getString(stateIndex));
Log.i("id", Integer.toString(c.getInt(idIndex)));
c.moveToNext();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
});
错误记录
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: INSERT INTO allUsers(name, zipcode, state) VALUES ( , , )
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: #################################################################
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: Error Code : 1 (SQLITE_ERROR)
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: Caused By : SQL(query) error or missing database.
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: (near ",": syntax error (code 1): , while compiling: INSERT INTO allUsers(name, zipcode, state) VALUES ( , , ))
>06-20 17:21:38.498 5433-5433/com.haasith.creation.jantan W/System.err: #################################################################
答案 0 :(得分:0)
这是由:
引起的final String fullNameText = fullName.getText().toString();
final String stateText = state.getText().toString();
final String zipcodeText = zipcode.getText().toString();
以上代码获取空字符串,因为您的OnClickListener
未获得最新的fullNameText
,stateText
和zipcodeText
值。
正确用法可能如下:
saveUserInfo.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
final String fullNameText = fullName.getText().toString();
final String stateText = state.getText().toString();
final String zipcodeText = zipcode.getText().toString();
userDB.execSQL("CREATE TABLE IF NOT EXISTS allUsers (name VARCHAR, zipcode VARCHAR, state VARCHAR, id INTEGER PRIMARY KEY)");
userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES ( " + fullNameText + ", " + zipcodeText + ", " + stateText + ")");
答案 1 :(得分:-1)
这是您的INSERT SQL
INSERT INTO allUsers(name, zipcode, state) VALUES ( " + fullNameText + ", " + zipcodeText + ", " + stateText + ")
结果字符串
INSERT INTO allUsers(name, zipcode, state) VALUES ( Name, ZipCode, StateText)
但是,您需要在SQL中引用该字符串。
INSERT INTO allUsers(name, zipcode, state) VALUES ( 'Name', 'ZipCode', 'StateText')
答案 2 :(得分:-1)
userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES ( " + fullNameText + ", " + zipcodeText + ", " + stateText + ")");
问题是这个SQL查询。它需要你3个值,基本上是名称,邮政编码和状态。您在“值”部分中的语法错误。它必须是
userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES ( fullNameText, zipcodeText, stateText)");
编辑: 如果你想在每个值之前和之后想要空格,你可以写它。就像这样:" " + fullNameText +" "等等