0

我是 android 开发领域的新手..当我运行我的代码时,它显示表联系人没有名为 phoneno: 的列,而编译:INSERT OR IGNORE INTO contacts(last_name, first_name, user_id, phoneno) VALUES(?, ?, ?, ?); 这是我的代码和日志猫显示错误..

OMGdatabase.java

package com.chat.omg;
import java.util.ArrayList;
import java.util.List;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class omgdatabase 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
    static final String KEY_ID = "_id";
    static final String KEY_USER_ID = "user_id";
    static final String KEY_FIRST_NAME = "first_name";
     static final String KEY_LAST_NAME = "last_name";
 static final String KEY_PH_NO = "phoneno";


    public omgdatabase(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 AUTOINCREMENT ," 
                + KEY_USER_ID + " INTEGER," 
                + KEY_FIRST_NAME + " TEXT,"
                + KEY_LAST_NAME + " TEXT,"
                + KEY_PH_NO + " INTEGER" + ")";
        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(omgcontact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_USER_ID, contact.user_id); // Contact userid
        values.put(KEY_FIRST_NAME, contact.firstname); // Contact first name
        values.put(KEY_LAST_NAME, contact.lastname); // Contact first name
        values.put(KEY_PH_NO, contact.phoneno); // Contact first name

        // Inserting Row
        db.insertWithOnConflict(TABLE_CONTACTS, null,
                values, SQLiteDatabase.CONFLICT_IGNORE);
        db.close(); // Closing database connection
    }
//
//  // Getting single contact
//  omgcontact getContact(int id) {
//      SQLiteDatabase db = this.getReadableDatabase();
//
//      Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,KEY_USER_ID,
//      KEY_LAST_NAME,KEY_FIRST_NAME, KEY_PH_NO }, KEY_ID + "=?",
//              new String[] { String.valueOf(id) }, null, null, null, null);
//      if (cursor != null)
//          cursor.moveToFirst();
//
//      omgcontact contact = new Contact(Integer.parseInt(cursor.getString(0)),
//              cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4));
//      // 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));
//              // Adding contact to list
//              contactList.add(contact);
//          } while (cursor.moveToNext());
//      }
//
//      // return contact list
//      return contactList;
//  }
//

//getting all contacts


    public Cursor getallcontact() {
        SQLiteDatabase db = this.getWritableDatabase();
       // List<omgcontact> allcontact = new ArrayList<omgcontact>();

        String[]columns ={KEY_ID,KEY_USER_ID,KEY_FIRST_NAME,KEY_LAST_NAME,KEY_PH_NO };
        Cursor cursor = db.query(TABLE_CONTACTS, columns, null, null, null, null, null);

        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
//        omgcontact contact = new omgcontact();
//        contact.setuid(Integer.parseInt(cursor.getString(0)));
//        contact.setfname(cursor.getString(1));
//        contact.setlname(cursor.getString(2));
//        contact.setphoneno(Integer.parseInt(cursor.getString(3)));
//        allcontact.add(contact);
          cursor.moveToNext();
        }
        // Make sure to close the cursor
        cursor.close();
        return cursor;
      } 
    // Updating single contact

    public int updateContact(omgcontact contact) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_FIRST_NAME, contact.firstname); // Contact first name
        values.put(KEY_LAST_NAME, contact.lastname); // Contact last name

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

    // Deleting single contact
    public void deleteContact(omgcontact contact) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_CONTACTS, KEY_PH_NO + " = ?",
                new String[] { String.valueOf(contact.phoneno) });
        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();
    }


}



omglistcontacts.java

package com.chat.omg;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;

public class omgListcontacts extends Activity{

    @SuppressWarnings("static-access")
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listinfo);
        SimpleCursorAdapter dataAdapter;
     omgdatabase datasource = new omgdatabase(this);

        Cursor cursor = datasource.getallcontact();
        String[]columns ={datasource.KEY_ID,datasource.KEY_FIRST_NAME,datasource.KEY_LAST_NAME,datasource.KEY_PH_NO };
        int[] to = new int[] { 
                R.id.fname,
                R.id.lname,
                R.id.phoneno,
              };

        // Use the SimpleCursorAdapter to show the
        // elements in a ListView
        dataAdapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_1,cursor,columns,to,0);
        ListView listview = (ListView)findViewById(R.id.listMode);
        listview.setAdapter(dataAdapter);


//      requestWindowFeature(Window.FEATURE_NO_TITLE);
//      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//          setListAdapter(new ArrayAdapter<omgcontact>(this,android.R.layout.simple_list_item_1, values));
//  
    }



}

原木猫

07-04 13:02:42.492: E/AndroidRuntime(4466):     at java.lang.Thread.run(Thread.java:1019)
07-04 13:02:42.492: E/AndroidRuntime(4466): Caused by: android.database.sqlite.SQLiteException: table contacts has no column named phoneno: , while compiling: INSERT OR IGNORE  INTO contacts(last_name, first_name, user_id, phoneno) VALUES(?, ?, ?, ?);
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at com.chat.omg.omgdatabase.addContact(omgdatabase.java:75)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at com.chat.omg.Onomgcontacts.favouritescontacts(Onomgcontacts.java:93)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at com.chat.omg.Onomgcontacts$getContacts.doInBackground(Onomgcontacts.java:54)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at com.chat.omg.Onomgcontacts$getContacts.doInBackground(Onomgcontacts.java:1)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-04 13:02:42.492: E/AndroidRuntime(4466):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
4

0 回答 0