如何解决错误:sqlite返回:错误代码= 1,msg =表DetailsCustomer没有名为Username的列,

时间:2012-05-24 11:08:27

标签: android sqlite

public class DBAdapter {
    public static final String COLUMN_ID = "ID";
    public static final String COLUMN_NAME = "NAME";
    public static final String COLUMN_SURNAME = "SURNAME";
    public static final String COLUMN_DATE_OF_BIRTH = "DateOfBirth";
    public static final String COLUMN_ADDRESS = "HomeAddress";
    public static final String COLUMN_EMAIL = "EmailNO";

    public static final String COLUMN_PHONE_NUMBER = "PhoneNumber";
    public static final String COLUMN_CITY = "City";
    public static final String COLUMN_PTYE_PAYMENT = "TypePayment";
    public static final String COLUMN_SHIPPING_TYPE = "ShippingType";
    public static final String COLUMN_CARD_NUMBER = "CardNumber";   
    public static final String COLUMN_username = "Username";
    public static final String COLUMN_Password = "Passwords";   

    public static final String TAG = "DBAdapter";

   private static final String DATABASE_NAME = "Computer_Info.db";
   private static final String USERS_TABLE = "DetailsCustomer";
   private static final int DATABASE_VERSION = 3;

private final Context context;

private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter (Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    DatabaseHelper(Context context) {
         super (context,DATABASE_NAME,null,DATABASE_VERSION);
    }

@Override
 public void onCreate(SQLiteDatabase db)
{



try {
          //db.execSQL(DATABASE_CREATE);        
         db.execSQL("CREATE TABLE "+ USERS_TABLE + 
                    "("+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    COLUMN_NAME + " TEXT, " + COLUMN_SURNAME +" TEXT, " +COLUMN_DATE_OF_BIRTH+

                   " TEXT NOT NULL, " + COLUMN_ADDRESS + " TEXT, " + COLUMN_EMAIL +" TEXT, " + 
                   COLUMN_PHONE_NUMBER + " TEXT, "+ COLUMN_CITY + " TEXT, "+ COLUMN_PTYE_PAYMENT +
                   " TEXT, " + COLUMN_CARD_NUMBER + " TEXT,"+ COLUMN_username + "TEXT ," + COLUMN_Password +" TEXT)");

} 



 catch (SQLException e) {
           e.printStackTrace();

       }
    }

public long insertRecord(String NAME, String SURNAME, String DateOfBirth,
         String HomeAddress,String EmailNO,  String PhoneNumber,  String City,
         long l, long m,String CardNumber,String Username, String Passwords) {
    {


            ContentValues initialValues = new ContentValues();


    initialValues.put(COLUMN_NAME,NAME);
    initialValues.put(COLUMN_SURNAME,SURNAME);
    initialValues.put(COLUMN_DATE_OF_BIRTH,DateOfBirth);
    initialValues.put(COLUMN_ADDRESS,HomeAddress);
    initialValues.put(COLUMN_EMAIL,EmailNO);
    initialValues.put(COLUMN_PHONE_NUMBER,PhoneNumber);
    initialValues.put(COLUMN_CITY,City);
    initialValues.put(COLUMN_PTYE_PAYMENT,l);
    initialValues.put(COLUMN_SHIPPING_TYPE,m);
    initialValues.put(COLUMN_CARD_NUMBER,CardNumber);
    initialValues.put(COLUMN_username,Username);
    initialValues.put(COLUMN_Password,Passwords);

            return db.insert(USERS_TABLE,null, initialValues);

       }

   }

public long insertRecord(String nAME, String sURNAME, String dateOfBirth,
        String homeAddress, String emailNO, String phoneNumber, String city,
        String string, String string2, String cardNumber, String Username, String Passwords) {
    return 0;   

    }

以及我的Activity类--------------------------

public void buttonRegister(View v)
    {
        Log.d("test", "adding"); 

        //get data from form
        EditText nametext = (EditText)findViewById(R.id.editTextName);
        EditText Surnametext = (EditText)findViewById(R.id.editTextSurname);
        EditText DOBtext = (EditText)findViewById(R.id.editTextDateBirth);
        EditText Adresstext = (EditText)findViewById(R.id.editTexAddress);
        EditText EmailNotext = (EditText)findViewById(R.id.editTeEmail);
        EditText Phonetext = (EditText)findViewById(R.id.editTexPhone);
        EditText Citytext = (EditText)findViewById(R.id.editTextCITY);
        Spinner PaymentSpinner = (Spinner)findViewById(R.id.spinnerPayment);
        Spinner ShippngMethod = (Spinner)findViewById(R.id.spinnerShipping);
        EditText CardNumbertext = (EditText)findViewById(R.id.editTextCARDNUMBER);
        EditText usernametext = (EditText)findViewById(R.id.txtusername);
        EditText pasword = (EditText)findViewById(R.id.editTexpassword);




db.open();
        long id = db.insertRecord(nametext.getText().toString(), Surnametext.getText().toString(),
                                   DOBtext.getText().toString(),Adresstext.getText().toString(),EmailNotext.getText().toString(),
                                   Phonetext.getText().toString(), Citytext.getText().toString(),
                                   PaymentSpinner.getSelectedItemId(), ShippngMethod.getSelectedItemId(), CardNumbertext.getText().toString(),
                                   usernametext.getText().toString(), pasword.getText().toString());




db.close();

        nametext.setText("");
        Surnametext.setText("");
        DOBtext.setText("");
        Adresstext.setText("");
        EmailNotext.setText("");
        Phonetext.setText("");     
        Citytext.setText("");
        PaymentSpinner.setId(0);
        ShippngMethod.setId(0);
        CardNumbertext.setText("");
        usernametext.setText("");
        pasword.setText("");





Toast.makeText(Regist.this,"Customer has been Registered",Toast.LENGTH_LONG).show();       

       }

我正在尝试插入数据库,但它告诉我列名中的用户名...我不知道为什么会这样,请帮帮我

2 个答案:

答案 0 :(得分:2)

这是正确的,因为您创建数据库的字符串中的这一位不存在该列:

COLUMN_username + "TEXT ," 

你错过了一个空间。它应该是:

COLUMN_username + " TEXT ," 

答案 1 :(得分:0)

更改数据库结构时,是否更改了

DATABASE_VERSION = 3;

值? 如果更改数据库版本,还需要覆盖

public void onUpdate(SQLiteDatabase db)

更新数据库的功能。