SQLiteDatabase错误

时间:2013-12-14 23:53:45

标签: android database sqlite

我目前正在尝试向数据库添加数据,但是当我尝试启动应用程序崩溃时,我收到一条错误消息,指出该表不存在。

以下是我创建数据库的代码:

package com.example.android_ca2;

import java.util.ArrayList;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "MyDBName.db";
public static final String PEOPLE_TABLE_NAME = "people";
public static final String PEOPLE_COLUMN_ID = "id";
public static final String PEOPLE_COLUMN_NAME = "name";
public static final String PEOPLE_COLUMN_AGE = "age";
public static final String PEOPLE_COLUMN_HEIGHT = "height";
public static final String PEOPLE_COLUMN_WEIGHT = "weight";


public DBHelper(Context context)
{
  super(context, DATABASE_NAME , null, 1);
}

@Override
public void onCreate(SQLiteDatabase db) {
  // TODO Auto-generated method stub
  db.execSQL(
  "create table " + PEOPLE_TABLE_NAME +
  "(id integer primary key, name text,age text,height text, weight text)"
  );
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // TODO Auto-generated method stub
  db.execSQL("DROP TABLE IF EXISTS " + PEOPLE_TABLE_NAME);
  onCreate(db);
}

public boolean insertPerson  (String name, String age, String height, String weight)
{
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();

  contentValues.put("name", name);
  contentValues.put("age", age);
  contentValues.put("height", height);  
  contentValues.put("weight", weight);

  db.insert("people", null, contentValues);
  return true;
}
public Cursor getData(int id){
  SQLiteDatabase db = this.getReadableDatabase();
  Cursor res =  db.rawQuery( "select * from people where id="+id+"", null );
  return res;
}
public int numberOfRows(){
  SQLiteDatabase db = this.getReadableDatabase();
  int numRows = (int) DatabaseUtils.queryNumEntries(db, PEOPLE_TABLE_NAME);
  return numRows;
}
public boolean updatePerson (Integer id, String name, String age, String height, String   weight)
{
  SQLiteDatabase db = this.getWritableDatabase();
  ContentValues contentValues = new ContentValues();
  contentValues.put("name", name);
  contentValues.put("phone", age);
  contentValues.put("email", height);
  contentValues.put("street", weight);

  db.update("people", contentValues, "id = ? ", new String[] { Integer.toString(id) }   );
  return true;
}

public Integer deletePerson (Integer id)
{
  SQLiteDatabase db = this.getWritableDatabase();
  return db.delete("people", 
  "id = ? ", 
  new String[] { Integer.toString(id) });
}

public ArrayList<String> getAllPeople()
{
  ArrayList<String> array_list = new ArrayList<String>();

  SQLiteDatabase db = this.getReadableDatabase();
  Cursor res =  db.rawQuery( "select * from " + PEOPLE_TABLE_NAME, null );
  res.moveToFirst();
  while(res.isAfterLast() == false){
  array_list.add(res.getString(res.getColumnIndex(PEOPLE_COLUMN_NAME)));
  res.moveToNext();
  }
return array_list;
}
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

如果您收到的错误表明该表不存在,那么您可以确定您尝试访问的表不在数据库中。

您最有可能在应用程序生命周期的早期阶段添加该表,但之后没有更新数据库版本,因此从未调用onUpgrade。

增加版本号或卸载应用程序,以便onCreate再次运行。