我必须开发一个跌倒探测器应用程序,我决定创建一个数据库来保存它的相关信息。但是当应用程序必须写入数据库时,logcat上会出现错误。这是logcat错误
05-12 06:14:33.892: E/Database(2724): Error inserting NCadute=0 DataInizio=2015/5/12 Durata=0:0:0 OraInizio=6:14:30 Nome=gshsgsusg
05-12 06:14:33.892: E/Database(2724): android.database.sqlite.SQLiteException: table Sessione has no column named NCadute: , while compiling: INSERT INTO Sessione(NCadute, DataInizio, Durata, OraInizio, Nome) VALUES(?, ?, ?, ?, ?);
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
05-12 06:14:33.892: E/Database(2724): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
05-12 06:14:33.892: E/Database(2724): at it.unipd.dei.rilevatoredicadute.MyDBManager.addSessione(MyDBManager.java:37)
05-12 06:14:33.892: E/Database(2724): at it.unipd.dei.rilevatoredicadute.Third$1.onClick(Third.java:47)
05-12 06:14:33.892: E/Database(2724): at android.view.View.performClick(View.java:2408)
05-12 06:14:33.892: E/Database(2724): at android.view.View$PerformClick.run(View.java:8818)
05-12 06:14:33.892: E/Database(2724): at android.os.Handler.handleCallback(Handler.java:587)
05-12 06:14:33.892: E/Database(2724): at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 06:14:33.892: E/Database(2724): at android.os.Looper.loop(Looper.java:123)
05-12 06:14:33.892: E/Database(2724): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-12 06:14:33.892: E/Database(2724): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 06:14:33.892: E/Database(2724): at java.lang.reflect.Method.invoke(Method.java:521)
05-12 06:14:33.892: E/Database(2724): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-12 06:14:33.892: E/Database(2724): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-12 06:14:33.892: E/Database(2724): at dalvik.system.NativeStart.main(Native Method)
这是sqliteopenhelper的代码java
package it.unipd.dei.rilevatoredicadute;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 5;
private static final String DATABASE_NAME = "progettoDB.db";
public static final String TABLE_SESSIONE = "Sessione";
public static final String TABLE_CADUTA="Caduta";
public static final String COL_IDS = "_id";
public static final String COL_NOME = "Nome";
public static final String COL_DATA = "DataInizio";
public static final String COL_ORA="OraInizio";
public static final String COL_DURATA = "Durata";
public static final String COL_NCADUTE = "NCadute";
public static final String COL_IDC="_id";
public static final String COL_DATAC = "DataCaduta";
public static final String COL_ORAC = "OraCaduta";
public static final String COL_LAT = "Latitudine";
public static final String COL_LON = "Longitudine";
public static final String COL_SESS = "NSessione";
public MyDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_SESSIONE_TABLE = " CREATE TABLE " +
TABLE_SESSIONE + "("
+ COL_IDS + " INTEGER PRIMARY KEY, "
+ COL_NOME + " TEXT NOT NULL, "
+ COL_DATA + " TEXT NOT NULL, "
+ COL_ORA + " TEXT NOT NULL, "
+ COL_DURATA + " TEXT NOT NULL, "
+ COL_NCADUTE + " INTEGER NOT NULL" + ");";
db.execSQL(CREATE_SESSIONE_TABLE);
/*String CREATE_CADUTA_TABLE = "CREATE TABLE " +
TABLE_CADUTA + "("
+ COL_IDC + " INTEGER PRIMARY KEY," + COL_DATAC
+ " TEXT NOT NULL," + COL_ORAC + " TEXT NOT NULL,"
+ COL_LAT + "TEXT NOT NULL," + COL_LON + "TEXT NOT NULL,"
+ COL_SESS + "TEXT NOT NULL" + ")";
db.execSQL(CREATE_CADUTA_TABLE);*/
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_SESSIONE);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CADUTA);
onCreate(db);
}
}
这是我管理数据库的代码java(MyDBManager.java)
package it.unipd.dei.rilevatoredicadute;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.ContentValues;
import android.database.sqlite.SQLiteException;
public class MyDBManager{
private MyDBHelper dbhelper;
public MyDBManager(Context ctx){
dbhelper=new MyDBHelper(ctx);
//SQLiteDatabase db = dbhelper.getWritableDatabase();
}
public void close(){
dbhelper.close();
}
public void addSessione(String nome, String data, String ora, String durata ,int ncadute){
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(MyDBHelper.COL_NOME, nome);
cv.put(MyDBHelper.COL_DATA, data);
cv.put(MyDBHelper.COL_ORA, ora);
cv.put(MyDBHelper.COL_DURATA, durata);
cv.put(MyDBHelper.COL_NCADUTE, ncadute);
db.insert(MyDBHelper.TABLE_SESSIONE, null, cv);
}
public void addCaduta(String data, String ora, String lat, String lon, String sessione){
SQLiteDatabase db = dbhelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(MyDBHelper.COL_DATAC, data);
cv.put(MyDBHelper.COL_ORAC, ora);
cv.put(MyDBHelper.COL_LAT, lat);
cv.put(MyDBHelper.COL_LON, lon);
cv.put(MyDBHelper.COL_SESS, sessione);
db.insert(MyDBHelper.TABLE_CADUTA, null, cv);
}
public boolean deleteSessione(int id){
SQLiteDatabase db = dbhelper.getWritableDatabase();
try{
if(db.delete(MyDBHelper.TABLE_SESSIONE, MyDBHelper.COL_IDS + "=?", new String[]{Integer.toString(id)})> 0)
return true;
return false;
}
catch(SQLiteException sqle){
return false;
}
}
public boolean renameSessione(String newName, int id)
{
SQLiteDatabase db=dbhelper.getWritableDatabase();
String RENAME_SESSIONE = "UPDATE" + MyDBHelper.TABLE_SESSIONE + "SET" + MyDBHelper.COL_NOME + "="
+ newName + "WHERE"+ MyDBHelper.COL_IDS + "= ?" + id + "";
db.execSQL(RENAME_SESSIONE);
return true;
}
public Cursor selectSessione(){
Cursor crs=null;
try
{
SQLiteDatabase db=dbhelper.getReadableDatabase();
crs=db.query(MyDBHelper.TABLE_SESSIONE, null, null, null, null, null, null, null);
}
catch(SQLiteException sqle)
{
return null;
}
return crs;
}
public Cursor selectCaduta()
{
Cursor crs=null;
try
{
SQLiteDatabase db=dbhelper.getReadableDatabase();
crs=db.query(MyDBHelper.TABLE_CADUTA, null, null, null, null, null, null, null);
}
catch(SQLiteException sqle)
{
return null;
}
return crs;
}
}
这是mainactivity java,我打开db然后关闭它
package it.unipd.dei.rilevatoredicadute;
import java.util.LinkedList;
import java.util.List;
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.database.Cursor;
public class MainActivity extends ActionBarActivity {
MyDBManager db= new MyDBManager(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
List<Dati> list = new LinkedList<Dati>();
Cursor crs=db.selectSessione();
if(crs.moveToFirst()){
String strData = crs.getString(crs.getColumnIndex("DataInizio"));
String[] dataf=strData.split("/");
int day=Integer.parseInt(dataf[0]);
int month=Integer.parseInt(dataf[1]);
int year=Integer.parseInt(dataf[2]);
String strTime = crs.getString(crs.getColumnIndex("OraInizio"));
String[] oraf=strTime.split(":");
int hour=Integer.parseInt(oraf[0]);
int minutes=Integer.parseInt(oraf[1]);
int seconds=Integer.parseInt(oraf[2]);
do{
list.add(new Dati(crs.getString(0),day, month, year,hour, minutes, seconds));
}while(crs.moveToNext());//fine while
}
else{
list.add(new Dati());
}
crs.close();
CustomAdapter adapter = new CustomAdapter(this, R.layout.list_items, list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> adapter, View v, int position, long id){
//Dati value = (Dati)adapter.getItemAtPosition(position);
Intent UI2 = new Intent(getApplicationContext(), Second.class);
startActivity(UI2);
}
});
//db.close();
}
@Override
protected void onDestroy()
{
super.onDestroy();
if (db != null)
{
db.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu){
super.onCreateOptionsMenu(menu);
MenuItem meIt1 = menu.add(0, R.id.nuovaSessione, 1, "Nuova Sessione");
MenuItem meIt2 = menu.add(0, R.id.delete, 2, "Elimina");
MenuItem meIt3 = menu.add(0, R.id.rinomina, 3, "Rinomina");
MenuItem meIt4 = menu.add(0, R.id.preferenze, 4, "Preferenze");
meIt1.setIntent(new Intent(this, Third.class));
meIt4.setIntent(new Intent(this, Fifth.class));
meIt3.setIntent(new Intent(this, Sixth.class));
return true;
}
}
至少这是我在数据库中插入raw的代码java(Third.java)
package it.unipd.dei.rilevatoredicadute;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.content.Context;
import android.content.Intent;
import java.util.GregorianCalendar;
public class Third extends ActionBarActivity {
Dati da = new Dati();
GregorianCalendar cal = new GregorianCalendar();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
final EditText et = (EditText)findViewById(R.id.insTesto);
final ImageButton playBtn = (ImageButton)findViewById(R.id.start);
final ImageButton pauseBtn = (ImageButton)findViewById(R.id.pause);
final ImageButton stopBtn = (ImageButton)findViewById(R.id.stop);
pauseBtn.setVisibility(View.INVISIBLE);
playBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//boolean play=true;
//if(play){
playBtn.setVisibility(View.INVISIBLE);
pauseBtn.setVisibility(View.VISIBLE);
da.setData(cal.get(GregorianCalendar.YEAR), cal.get(GregorianCalendar.MONTH)+1, cal.get(GregorianCalendar.DATE));
String data = da.getData();
da.setHour(cal.get(GregorianCalendar.HOUR_OF_DAY), cal.get(GregorianCalendar.MINUTE), cal.get(GregorianCalendar.SECOND));
String ora = da.getHour();
MyDBManager db = new MyDBManager(getApplicationContext());
db.addSessione(et.getText().toString(), data, ora, "0:0:0", 0);
db.close();
/*play=false;
}
else{
playBtn.setVisibility(View.VISIBLE);
pauseBtn.setVisibility(View.INVISIBLE);
}*/
}
});
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.third, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}*/
}
我遵循建议更改数据库版本和unistalling并重新安装设备上的应用程序(archos),但logcat给了我同样的问题。有人可以帮帮我吗? PS抱歉我的英文非常糟糕
答案 0 :(得分:1)
表格是第一次使用时创建的。如果添加另一个表,则无法创建此表,因为oncreate methot会在第一次使用时创建表。 uprage db version并使用onUprage覆盖方法删除表并调用oncreate方法