0

我正在尝试将editText中的一些数据保存到基于我的Android设备的SQLite数据库(它应该只基于设备)......所以我创建了DatabaseHelper类并在那里创建了数据库,这里是这个文件的代码:

package ru.vladimir.droid_spy;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper 
{

    public static final String TABLE_NAME = "phones";
    public static final String COLUMN_PHONES ="phone_number";

    private static final String DATABASE_NAME = "trusted_phone.db";
    private static final int DATABASE_VERSION = 1;

    //create database
    private static final String DATABASE_CREATE = "create table " + TABLE_NAME + " ( " + BaseColumns._ID + " integer primary key autoincrement, " + COLUMN_PHONES + " text not null);";

    public DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        // TODO Auto-generated method stub

        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    {
        // TODO Auto-generated method stub
        Log.w(DatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
            onCreate(db);
    }

}

然后我想用这段代码从我的活动中保存一些数据:

try 
{
    DatabaseHelper DBhelper = new DatabaseHelper(getBaseContext());
    //put DB in write mode
    SQLiteDatabase db = DBhelper.getWritableDatabase();
    //put variables
    ContentValues values = new ContentValues();
    //values.put(DatabaseHelper.COLUMN_ID, 1);
    values.put(DatabaseHelper.COLUMN_PHONES, txt_phone.getText().toString());

    //insert variables into DB
    long query = db.insert(DatabaseHelper.TABLE_NAME, null, values);

    //close DB
    db.close();
} 
catch(Exception e) 
{
    System.out.println("Error: "+e.getLocalizedMessage());
}

但是当我看到 LogCat 日志时,出现了一条错误消息:

07-27 00:04:50.463: E/SQLiteDatabase(15075): Error inserting phone_number=WEB
07-27 00:04:50.463: E/SQLiteDatabase(15075): android.database.sqlite.SQLiteException: table phones has no column named phone_number: , while compiling: INSERT INTO phones(phone_number) VALUES (?)

我认为这个错误说明我没有在我的数据库中创建一个列,但是如果我在CREATE_DATABASE变量中创建这个列,我怎么不能创建它?

4

2 回答 2

0

我只是在这里猜测,但可能是您之前使用不同的表结构运行了该应用程序?由于您的数据库版本仍然是 1,因此onUpgrade()永远不会被调用。也没有,onCreate()因为数据库已经存在。要么增加数据库版本,要么卸载并重新安装应用程序以确保数据库是新创建的。

于 2013-07-26T21:19:19.863 回答
0
public class Sqlitedatabase extends SQLiteOpenHelper {

  public Sqlitedatabase(Context context) {
      super(context, "DoctorDatabase.db", null, 1);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
      String tableEmp = "create table Doctor(Name text,Number text)";
      db.execSQL(tableEmp);
  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  }

  public void insertData(DocotorInformation doctor) {
      SQLiteDatabase db = this.getWritableDatabase();

      ContentValues values = new ContentValues();
      values.put("Name", doctor.getName()); // Contact Name
      values.put("Number", doctor.getNumber()); // Contact Phone Number

      // Inserting Row
      db.insert("Doctor", null, values);
      db.close(); // Closing database connection
  }


  public ArrayList<DocotorInformation> getAllContacts() {
      ArrayList<DocotorInformation> contactList = new ArrayList<DocotorInformation>();
      // Select All Query
      String selectQuery = "SELECT  * FROM Doctor";

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

      // looping through all rows and adding to list
      if (cursor.moveToFirst()) {
          do {
            DocotorInformation contact = new DocotorInformation();
            contact.setName(cursor.getString(0));
            contact.setNumber(cursor.getString(1));

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

    // return contact list
    return contactList;
    }
}
于 2017-02-09T04:30:31.397 回答