2013-07-04 5 views
0

내 안드로이드 응용 프로그램에서 SQLite db를 구현하고 있습니다. JSON에서 데이터를 가져 와서 SQLite db에 저장하고 싶습니다. SQLite를 사용하는 예제 중 하나를 보았지만 다른 열 이름 인 wchi를 사용하고 있습니다. SQLite db에 저장합니다.안드로이드에서 SQLite db에 데이터를 삽입하는 방법은 무엇입니까?

하지만 난 내가 좋아하는 오류를 받고 있어요 응용 프로그램 =

((1) 테이블 연락처가 비용라는 이름의 열,

오류 삽입 PHONE_NUMBER = 9,533,333,333 비용 = 46,456 명 = KARTHIK을 실행할 때 ,

android.database.sqlite.SQLiteException

: (???) : 컴파일하는 동안, : 테이블 연락처 럼 이름 비용 (코드 1) 없음) 연락처 (PHONE_NUMBER, 비용, 이름) 값으로 INSERT

public class DatabaseHandler extends SQLiteOpenHelper { 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 1; 

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

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

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

    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_COST + " TEXT," 
       + KEY_PH_NO + " 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); 
    } 

    /** 
    * All CRUD(Create, Read, Update, Delete) Operations 
    */ 

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

     ContentValues values = new ContentValues(); 
     values.put(KEY_NAME, contact._name); // Contact Name 
     values.put(KEY_COST, contact._cost); // Contact Phone 
     values.put(KEY_PH_NO, contact._mobile); //Contact phone no 

     // Inserting Row 
     db.insert(TABLE_CONTACTS, null, values); 
     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_COST, 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.set_id(Integer.parseInt(cursor.getString(0))); 
       contact.set_name(cursor.getString(1)); 
       contact.set_cost(cursor.getString(2)); 
       contact.set_mobile(cursor.getString(3)); 
       contactList.add(contact); 
      } while (cursor.moveToNext()); 
     } 

     // return contact list 
     return contactList; 
    } 

    // Updating single contact 
    public int updateContact(Contact contact) { 
     SQLiteDatabase db = this.getWritableDatabase(); 

     ContentValues values = new ContentValues(); 
     values.put(KEY_NAME, contact.get_name()); 
     values.put(KEY_COST, contact.get_cost()); 
     values.put(KEY_PH_NO, contact.get_mobile()); 

     // updating row 
     return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?", 
       new String[] { String.valueOf(contact.get_id()) }); 
    } 

    // Deleting single contact 
    public void deleteContact(Contact contact) { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     db.delete(TABLE_CONTACTS, KEY_ID + " = ?", 
       new String[] { String.valueOf(contact.get_id()) }); 
     db.close(); 
    } 


    // Getting contacts Count 
    public int getContactsCount() { 
     String countQuery = "SELECT * FROM " + TABLE_CONTACTS; 
     SQLiteDatabase db = this.getReadableDatabase(); 
     Cursor cursor = db.rawQuery(countQuery, null); 
     cursor.close(); 

     // return count 
     return cursor.getCount(); 
    } 

} 
+0

이 작업이 이미 성공적으로 실행 되었습니까? – R9J

답변

1

변경이

private static final int DATABASE_VERSION = 2; 

이 라인

private static final int DATABASE_VERSION = 1; 

그리고 다시 작업을 확인합니다.

0

앱 데이터를 지우고 앱을 다시 실행 해보십시오. 이전에 다른 열이 있었을 수도 있고 앱을 한 번 실행할 수도 있습니다. 즉, create table이 다시 호출되지 않음을 의미합니다.

관련 문제