2017-10-14 12 views
-3

私はAndroidアプリケーション用の簡単なSQLiteデータベースを作成しています。私はthis tutorialに従った。このチュートリアルを使用して私は3つの文字列の値、すなわち "名前"、 "アドレス"、および "電話番号"をデータベースに追加しようとしています。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 

にデータを入れていたコードですこれら3つの変数はすべてストリングです。

しかし、私はアプリを実行すると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

onCreateメソッドのSQL作成スクリプトにエラー(カンマがない)があり、KEY_ADRESS列が作成されていません。

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

は次のようになります。

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

はい、それが働きました。私がやらなければならなかったのは、最初に以前のバージョンのアプリをアンインストールして、新しい列をデータベースに追加するときに再インストールすることだけでした –

関連する問題