2017-10-14 28 views
-3

我正在爲我的android應用程序開發一個簡單的SQLite數據庫。我跟着this tutorial。使用本教程,我試圖向我的數據庫添加三個字符串值,即「name」,「adress」和「phonenumber」。SQLite數據庫:插入名稱,地址,電話號碼時出錯

這是DatabaseHandler.java

// Database Version 
private static final int DATABASE_VERSION = 1; 

// Database Name 
private static final String DATABASE_NAME = "favouritesmanager"; 

// Contacts table name 
private static final String TABLE_CONTACTS = "favourites"; 

// Contacts Table Columns names 
private static final String KEY_ID = "id"; 
private static final String KEY_NAME = "name"; 
private static final String KEY_PH_NO = "phone_number"; 
private static final String KEY_ADRESS = "adress"; 


public DatabaseHandler(Context context) { 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 

// Creating Tables 
@Override 
public void onCreate(SQLiteDatabase db) { 
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "(" 
      + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," 
      + KEY_PH_NO + " TEXT" + KEY_ADRESS + " TEXT" + ")"; 
    db.execSQL(CREATE_CONTACTS_TABLE); 
} 

// Upgrading database 
@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // Drop older table if existed 
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS); 

    // Create tables again 
    onCreate(db); 
} 

// Adding new contact 
public void addContact(Contact contact) { 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues values = new ContentValues(); 
    values.put(KEY_NAME, contact.getName()); // Contact Name 
    values.put(KEY_PH_NO, contact.getPhoneNumber()); // Contact Phone Number 
    values.put(KEY_ADRESS, contact.getAdress()); //address 

    // Inserting Row 
    db.insert(TABLE_CONTACTS, null, values); << Error here 
    db.close(); // Closing database connection 
} 


// Getting single contact 
Contact getContact(int id) { 
    SQLiteDatabase db = this.getReadableDatabase(); 

    Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID, 
        KEY_NAME, KEY_PH_NO }, KEY_ID + "=?", 
      new String[] { String.valueOf(id) }, null, null, null, null); 
    if (cursor != null) 
     cursor.moveToFirst(); 

    Contact contact = new Contact(Integer.parseInt(cursor.getString(0)), 
      cursor.getString(1), cursor.getString(2), cursor.getString(3)); 
    // return contact 
    return contact; 
} 

// Getting All Contacts 
public List<Contact> getAllContacts() { 
    List<Contact> contactList = new ArrayList<Contact>(); 
    // Select All Query 
    String selectQuery = "SELECT * FROM " + TABLE_CONTACTS; 

    SQLiteDatabase db = this.getWritableDatabase(); 
    Cursor cursor = db.rawQuery(selectQuery, null); 

    // looping through all rows and adding to list 
    if (cursor.moveToFirst()) { 
     do { 
      Contact contact = new Contact(); 
      contact.setID(Integer.parseInt(cursor.getString(0))); 
      contact.setName(cursor.getString(1)); 
      contact.setPhoneNumber(cursor.getString(2)); 
      contact.setAdress(cursor.getString(3)); 

      // Adding contact to list 
      contactList.add(contact); 
     } while (cursor.moveToNext()); 
    } 

    // return contact list 
    return contactList; 
} 

} 

和Contact.java類

public class Contact { 

    int _id; 
    String _name; 
    String _phone_number; 
    String _adress; 

    public Contact(){ 

    } 

    // constructor 
    public Contact(int id, String name, String _phone_number, String adress){ 
     this._id = id; 
     this._name = name; 
     this._phone_number = _phone_number; 
     this._adress = adress; 
    } 

    // constructor 
    public Contact(String name, String _phone_number, String adress){ 
     this._name = name; 
     this._phone_number = _phone_number; 
     this._adress = adress; 
    } 

    // getting ID 
    public int getID(){ 
     return this._id; 
    } 

    // setting id 
    public void setID(int id){ 
     this._id = id; 
    } 

    // getting name 
    public String getName(){ 
     return this._name; 
    } 

    // setting name 
    public void setName(String name){ 
     this._name = name; 
    } 

    // getting phone number 
    public String getPhoneNumber(){ 
     return this._phone_number; 
    } 

    // setting phone number 
    public void setPhoneNumber(String phone_number){ 
     this._phone_number = phone_number; 
    } 

    //getting adress 
    public String getAdress(){ 

     return this._adress; 
    } 

    //setting adress 
    public void setAdress(String adresstowrite){ 
     this._adress = adresstowrite; 
    } 
} 

下面的代碼是我在哪裏把數據存入數據庫

DatabaseHandler db = new DatabaseHandler(this); 
db.addContact(new Contact(registeredname,registerdphone,adress)); << Error here 

代碼所有這三個變量都是字符串。

但是當我運行的應用程序,並檢查了android監視器它顯示了以下錯誤

E/SQLiteDatabase: Error inserting name=yy adress=checking adress phone_number=08335565 
                   android.database.sqlite.SQLiteException: table favourites has no column named adress (code 1): , while compiling: INSERT INTO favourites(name,adress,phone_number) VALUES (?,?,?) 
                    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) 
                    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:908) 
                    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:509) 
                    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:1527) 
                    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1399) 
                    at digiart.mapwithfirebase.DatabaseHandler.addContact(DatabaseHandler.java:67) // here 
                    at digiart.mapwithfirebase.MapsActivity.favourite(MapsActivity.java:301) // and here 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at java.lang.reflect.Method.invoke(Method.java:372) 

任何人幫助嗎?

回答

1

SQL創建腳本onCreate方法中的錯誤(缺失逗號)並且KEY_ADRESS列未創建。

+ KEY_PH_NO + " TEXT" + KEY_ADRESS + " TEXT" + ")"; 

應該是:

+ KEY_PH_NO + " TEXT, " + KEY_ADRESS + " TEXT" + ")"; 
+0

是它的工作。我必須做的唯一事情就是先卸載先前版本的應用程序,然後重新安裝,因爲我在數據庫中添加了一個新列 –