Android SQLite Colunm未找到错误

时间:2014-08-25 16:13:50

标签: java android sqlite

我试图将来自某些editTexts的数据存储在本地android数据库上,然后最终在listview上显示它,但是现在我得到一个错误,说明列不存在,我正在关注android hive的教程所以一些变量名可能看起来很奇怪,但一切都应该准确。

栈跟踪

2038-12038/com.example.adrian.legioncheck_in E/SQLiteLog﹕ (1) table contacts has no column named Longitude
08-25 12:12:19.121  12038-12038/com.example.adrian.legioncheck_in E/SQLiteDatabase﹕ Error inserting Latitude=-85 Longitude=50 POnumber=255902
    android.database.sqlite.SQLiteException: table contacts has no column named Longitude (code 1): , while compiling: INSERT INTO contacts(Latitude,Longitude,POnumber) VALUES (?,?,?)
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:923)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:534)
            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:1523)
            at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1395)
            at com.example.adrian.legioncheck_in.DatabaseHandler.addContact(DatabaseHandler.java:67)
            at com.example.adrian.legioncheck_in.MapsActivity.SaveAction(MapsActivity.java:67)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3850)
            at android.view.View.performClick(View.java:4470)
            at android.view.View$PerformClick.run(View.java:18593)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5867)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
            at dalvik.system.NativeStart.main(Native Method)

Database Handle.java

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

    import java.util.ArrayList;
    import java.util.List;

    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 = "POnumber";
        private static final String KEY_PH_NO = "Latitude";
        private static final String KEY_LONG = "Longitude";

        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_LONG + " 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
/* line 67 */        void addContact(Contact contact) {
            SQLiteDatabase db = this.getWritableDatabase();

            ContentValues values = new ContentValues();
            values.put(KEY_NAME, contact.getPO());
            values.put(KEY_PH_NO, contact.getLat());
            values.put(KEY_LONG, contact.getLong());

            // 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_PH_NO, KEY_LONG }, 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.setPO(cursor.getString(1));
                    contact.setLat(cursor.getString(2));
                    contact.setLong(cursor.getString(3));
                    // Adding contact to list
                    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.getPO());
            values.put(KEY_PH_NO, contact.getLat());
            values.put(KEY_LONG, contact.getLong());

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

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

    }

contact.java

public class Contact {

    //private variables
    int _id;
    String _PO;
    String _lat;
    String _longi;

    // Empty constructor
    public Contact(){

    }
    // constructor
    public Contact(int id, String PO, String _lat, String _longi){
        this._id = id;
        this._PO = PO;
        this._lat = _lat;
        this._longi = _longi;
    }

    // constructor
    public Contact(String PO, String _lat, String _longi){
        this._PO = PO;
        this._lat = _lat;
        this._longi = _longi;
    }
    // getting ID
    public int getID(){
        return this._id;
    }

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

    // getting name
    public String getPO(){
        return this._PO;
    }

    // setting name
    public void setPO(String PO){
        this._PO = PO;
    }

    // getting phone number
    public String getLat(){
        return this._lat;
    }

    // setting phone number
    public void setLat(String lat){
        this._lat = lat;
    }
    public String getLong()
    {
        return this._longi;
    }
    public void setLong(String longi)
    {
        this._longi = longi;
    }
}

将数据保存到数据库的按钮:

public void SaveAction(View view)
    {
        EditText po = (EditText)findViewById(R.id.POnumber);
        String PO = po.getText().toString();
        EditText textlat = (EditText)findViewById(R.id.textLat);
        String LAT = textlat.getText().toString();
        EditText textlong = (EditText)findViewById(R.id.textLong);
        String LONG = textlong.getText().toString();
        DatabaseHandler db = new DatabaseHandler(MapsActivity.this);
        db.addContact(new Contact(PO,LAT,LONG));
        // Reading all Data
        Log.d("Reading: ", "Reading all inputs..");
        List<Contact> contacts = db.getAllContacts();

        for (Contact cn : contacts) {
            String log = "Id: "+cn.getID()+" ,PO number: " + cn.getPO() + " ,Latitude: " + cn.getLat() + " ,Longitude: " + cn.getLong();
            // Writing Data to log
            Log.d("Name: ", log);
        }
    }

1 个答案:

答案 0 :(得分:3)

KEY_PH_NO + " TEXT" + KEY_LONG + " TEXT" +")"

,之前添加缺失的KEY_LONG

KEY_PH_NO + " TEXT," + KEY_LONG + " TEXT" +")"

卸载您的应用,以便再次运行onCreate()。看到 When is SQLiteOpenHelper onCreate() / onUpgrade() run?  了解更多相关信息。