2016-06-21 7 views
-2

に文字列を挿入:sqliteのエラーは、私は誰かが明確化することを願っている「」私は<code>Mysql</code>デシベルに<code>EditText</code>から取得する値を挿入しようとしていますが、私は近くにエラーが発生しているデシベルアンドロイド

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

これはによって引き起こされる:あなたのOnClickListenerは、最新fullNameTextstateTextzipcodeText値を取得しないため

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 
     { 

     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 + ")"); 
+0

文字列はプリミティブ型ではなく、参照型です。あなたの答えは正しいです(彼は 'OnClickListener'の中で文字列を取得する必要があります)。 – Karakuri

+0

@ Karakuri、あなたは正しい、私は遠く最後になります。 – chengpohi

+0

logcatは最終的な文字列がカラム名であると考える> 06-21 00:42:46.0​​90 17205-17205/com.haasith.creation.jantan E/SQLiteLog:(jkjjk > 06-21 00:42: (名前、郵便番号、州)をコンパイル中に:INSERT INTO allUsers(名前、郵便番号、州):46.0​​90 17205-17205/com.haasith.creation.jantan W/System.err:android.database.sqlite.SQLiteException:いいえそのような列:jkjjk VALUES(jkjjk、989889、jjknj)@karakuri –

-1

あなたのINSERTのSQLが

ある
INSERT INTO allUsers(name, zipcode, state) VALUES (" + fullNameText + ", " + zipcodeText + ", " + stateText + ") 

結果STRこれは、

INSERT INTO allUsers(name, zipcode, state) VALUES (Name, ZipCode, StateText) 

ですが、SQLで文字列を引用する必要があります。

INSERT INTO allUsers(name, zipcode, state) VALUES ('Name', 'ZipCode', 'StateText') 
+0

私はedittextのテキストを "Name"という文字列以外の文字列に保存したいとします。 –

+0

@HaasithSanka文字列はこのようにしなければなりません "INSERT INTO allUsers(name、zipcode、state)VALUES( '" + fullNameText + '、' + zipcodeText + "'、+" + stateText + "')" ' 文字列に' ''を追加する必要があります。 答えの文字列は、** RESULT STRING **であり、ステートメント自体ではありません。 – Joshua

-1

userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES (" + fullNameText + ", " + zipcodeText + ", " + stateText + ")");

問題は、このSQLクエリです。それはあなたから3つの値、基本的に名前、郵便番号と状態が必要です。値のセクションで構文が間違っています。 そして、あなたはあなたがこのit.like書くことができますand.after各値の前に空白をしたい場合:それは

userDB.execSQL("INSERT INTO allUsers(name, zipcode, state) VALUES (fullNameText, zipcodeText, stateText)");

を編集する必要があり、 " "" + fullNameText +" などなど

+0

私はこれを試したときに、このエラー> android.database.sqliteを得ました。SQLiteException:そのような列はありません:fullNameText(コード1):コンパイル中:INSERT INTO allUsers(name、zipcode、state)VALUES(fullNameText、zipcodeText、stateText) - –

+0

http://www.tutorialspoint.com /jdbc/jdbc-insert-records.htmこのようなVALUES( "+ fullNameText +"など)としてクエリを追加すると、 – MrXplicit

関連する問題

 関連する問題