SQLite无法绑定索引1处的参数,因为索引超出范围

时间:2015-01-22 06:44:07

标签: android sqlite

这是我的第一篇文章,如果发布错误,我很抱歉。

我目前正在尝试学习一些简单的android开发,并遇到了一个我无法弄清楚的错误。我下载了一些只存储用户名和密码的简单登录教程代码。

下载时代码运行正常。当我向数据库添加一些条目时,我开始收到错误。我仍然可以创建一个帐户,但是当我尝试登录时,它表示信息不匹配。我已经做了一些测试,我认为数据库在创建帐户期间没有存储任何信息,即使我收到了插入值时显示的确认消息。我也在控制台中收到此错误:“java.lang.IllegalArgumentException:无法绑定索引1处的参数,因为索引超出范围。语句有0个参数。”

感谢任何帮助。这是我认为与此问题相关的java代码。第一个是我创建帐户的地方,第二个是我与数据库交互的地方。如果您需要其他代码,请告诉我。谢谢!

更新

这是我的logcat。当我尝试提交信息时会出现错误。

01-22 02:05:46.292    1141-1141/com.techblogon.loginexample E/SQLiteLog﹕ (1) near ",": syntax error
01-22 02:05:46.302    1141-1141/com.techblogon.loginexample E/SQLiteDatabase﹕ Error inserting FCOLOR,=red USERNAME=home CAT=cat PASSWORD=password DOG=dog
    android.database.sqlite.SQLiteException: near ",": syntax error (code 1): , while compiling: INSERT INTO LOGIN(FCOLOR,,USERNAME,CAT,PASSWORD,DOG) VALUES (?,?,?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
            at com.team47.techProf.LoginDataBaseAdapter.insertEntry(LoginDataBaseAdapter.java:56)
            at com.team47.techProf.SignUPActivity$1.onClick(SignUPActivity.java:75)
            at android.view.View.performClick(View.java:4438)
            at android.view.View$PerformClick.run(View.java:18422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
01-22 02:05:46.882    1141-1141/com.techblogon.loginexample W/EGL_emulation﹕ eglSurfaceAttrib not implemented


public class SignUPActivity extends Activity
{
    EditText editTextUserName,editTextPassword,editTextConfirmPassword,editTextDog,editTextFColor, editTextCat;
    Button btnCreateAccount;

    LoginDataBaseAdapter loginDataBaseAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.signup);

        // get Instance  of Database Adapter
        loginDataBaseAdapter=new LoginDataBaseAdapter(this);
        loginDataBaseAdapter=loginDataBaseAdapter.open();

        // Get References of Views
        editTextUserName=(EditText)findViewById(R.id.editTextUserName);
        editTextPassword=(EditText)findViewById(R.id.editTextPassword);
        editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword);
        editTextDog=(EditText)findViewById(R.id.editTextDog);
        editTextFColor=(EditText)findViewById(R.id.editTextFColor);
        editTextCat=(EditText)findViewById(R.id.editTextCat);

        btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount);
        btnCreateAccount.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            String userName=editTextUserName.getText().toString();
            String password=editTextPassword.getText().toString();
            String confirmPassword=editTextConfirmPassword.getText().toString();
            String dog=editTextDog.getText().toString().toLowerCase();
            String fcolor=editTextFColor.getText().toString().toLowerCase();
            String cat=editTextCat.getText().toString().toLowerCase();

            // check if any of the fields are vaccant
            if(userName.equals("")||password.equals("")||confirmPassword.equals("")||dog.equals("")||fcolor.equals("")||cat.equals(""))
            {
                    Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show();
                    return;
            }
            // check if both password matches
            if(!password.equals(confirmPassword))
            {
                Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show();
                return;
            }
            if (!dog.equals("dog"))
            {
                Toast.makeText(getApplicationContext(),"You did not type 'dog' correctly",Toast.LENGTH_LONG ).show();
                return;
            }
            if (!cat.equals("cat"))
            {
                Toast.makeText(getApplicationContext(),"You did not type 'cat' correctly",Toast.LENGTH_LONG ).show();
                return;
            }
            else
            {
                // Save the Data in Database
                loginDataBaseAdapter.insertEntry(userName, password, dog, fcolor, cat);
                Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
                finish();

            }
        }
    });
}
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();

        loginDataBaseAdapter.close();
    }
}

public class LoginDataBaseAdapter 
{
        static final String DATABASE_NAME = "login.db";
        static final int DATABASE_VERSION = 1;
        public static final int NAME_COLUMN = 1;
        // TODO: Create public field for each column in your table.
        // SQL Statement to create a new database.
        static final String DATABASE_CREATE = "create table "+"LOGIN"+
                                     "( " +"ID"+" integer primary key autoincrement,"+ "USERNAME  text,PASSWORD text, DOG text, FCOLOR text, CAT text ); ";
        // Variable to hold the database instance
        public  SQLiteDatabase db;
        // Context of the application using the database.
        private final Context context;
        // Database open/upgrade helper
        private DataBaseHelper dbHelper;
        public  LoginDataBaseAdapter(Context _context)
        {
            context = _context;
            dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public LoginDataBaseAdapter open() throws SQLException
        {
            db = dbHelper.getWritableDatabase();
            return this;
        }
        public void close() 
        {
            db.close();
        }

        public  SQLiteDatabase getDatabaseInstance()
        {
            return db;
        }

        public void insertEntry(String userName,String password, String dog, String fcolor, String cat )
        {
           ContentValues newValues = new ContentValues();
            // Assign values for each row.
            newValues.put("USERNAME", userName);
            newValues.put("PASSWORD",password);
            newValues.put("DOG",dog);
            newValues.put("FCOLOR,",fcolor);
            newValues.put("CAT",cat);

            // Insert the row into your table
            db.insert("LOGIN", null, newValues);
            //Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
        }
        public int deleteEntry(String UserName)
        {
            //String id=String.valueOf(ID);
            String where="USERNAME=?";
            int numberOFEntriesDeleted= db.delete("LOGIN", where, new String[]{UserName}) ;
           // Toast.makeText(context, "Number of Entry Deleted Successfully : "+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
            return numberOFEntriesDeleted;
        }   
        public String getPasswordEntry(String userName)
        {
            Cursor cursor=db.query("LOGIN", null, "USERNAME=?", new String[]{userName}, null, null, null);
            if(cursor.getCount()<1) // UserName Not Exist
            {
                cursor.close();
                return "NOT EXIST";
            }
            cursor.moveToFirst();
            String password= cursor.getString(cursor.getColumnIndex("PASSWORD"));
            cursor.close();
            return password;                
        }
        public void  updateEntry(String userName,String password)
        {
            // Define the updated row content.
            ContentValues updatedValues = new ContentValues();
            // Assign values for each row.
            updatedValues.put("USERNAME", userName);
            updatedValues.put("PASSWORD",password);

            String where="USERNAME = ?";
            db.update("LOGIN",updatedValues, where, new String[]{userName});               
        }       
}

1 个答案:

答案 0 :(得分:0)

您的查询中有一个空白列名称:

INSERT INTO LOGIN(FCOLOR,,USERNAME,CAT,PASSWORD,DOG) VALUES (?,...
                        ^^^ ^

看起来你插入的定义是错误的。