我无法在数据库中插入数据

时间:2014-03-17 10:34:03

标签: android sqlite

03-17 15:19:50.332: E/SQLiteDatabase(1565): Error inserting Password=xx User_name=xx 
03-17 15:19:50.332: E/SQLiteDatabase(1565): android.database.sqlite.SQLiteException: table login has no column named Password: , 
while compiling: INSERT INTO login(Password,User_name) VALUES (?,?)

我的数据库代码

db = context.openOrCreateDatabase("MyDb", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS login(User_id integer primary key autoincrement, User_name VARCHAR, Password VARCHAR )");

这是amy dbhelper代码   **插入数据代码**

public boolean InsertData(String Table_Name , ContentValues cv)
{
if(db.insert(Table_Name, null, cv) > 0)
{
return true ;
}
return false;
}

这是我的活动,我正在访问我已经在dbhelper类中构建的insert方法,我应该制作什么样的版本...

    bundle = getIntent().getExtras();
if (bundle != null) {
list = new ArrayList<login>();
//list= dbHelper.getUser();
user_name.setText(list.get(0).getUser_name());
password.setText(list.get(0).getPassword());



    }

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

        User_name = user_name.getText().toString();
        Password = password.getText().toString();

        if (User_name.length() > 0 && Password.length() > 0)
        {
            ContentValues cv = new ContentValues();
            cv.put("User_name", User_name);
            cv.put("Password", Password);

                if (SavingOptions.equalsIgnoreCase("Insert")) 
                {
                    if (dbHelper.InsertData("login", cv)) 
                    {
                        //list = dbHelper.getUser();    
Toast.makeText(LoginActivity.this,"Login Successfull", Toast.LENGTH_LONG).show();

                    }
                    else 
                    {
Toast.makeText(LoginActivity.this,"Login not Successfull", Toast.LENGTH_LONG).show();
                    }


                } 

        }
        else 
        {
Toast.makeText(LoginActivity.this, "Plz fill all indexes", Toast.LENGTH_LONG).show();

        }
        }
    });
}

2 个答案:

答案 0 :(得分:-1)

试试这个

db.execSQL(“CREATE TABLE IF NOT NOT EXISTS登录(_id整数主键自动增量,User_name TEXT,密码TEXT)”);

答案 1 :(得分:-2)

密码:
得到它
删除列条目的冒号

修改

    SQLiteDatabase db = instance.getWritableDatabase();

    // 2. create ContentValues and add column-value pairs
    ContentValues values = new ContentValues();

    values.put(USER_NAME, getUserName()); // get name
    values.put(PASSWORD, getPassword()); // get password

    // 3. insert
    db.insert(DBTABLE, // table
        null, // nullColumnHack
        values); // key/value -> keys = column names/ values = column values

    // 4. close
    db.close();

使用此模式插入更好的值并注意sql注入
值得注意的是,您正在存储密码