没有列错误:SQLite Android

时间:2014-04-12 12:28:16

标签: android android-sqlite

即使此应用程序中没有错误,每次要求Vactivity时它都会崩溃。 LogCat显示表Analysis_for_Today没有名为Activated的列。请帮助我找到我在这里做错了什么。

MainActivity类(首先推出):

public class MainActivity extends Activity{


long currentTimeMillis;
long endTimeMillis;
public int count = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    currentTimeMillis = System.currentTimeMillis();

    final MediaPlayer mp = MediaPlayer.create(this, R.drawable.beep);
    Button b = (Button) findViewById(R.id.button1);

    b.setOnClickListener(new View.OnClickListener(){

         public void onClick(View v) {
             mp.start();
             if(mp.isPlaying())
                count++;


             }
     });   


}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


@Override
protected void onStop() {
    // TODO Auto-generated method stub
    super.onStop();
    endTimeMillis = System.currentTimeMillis();
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("on",String.valueOf(currentTimeMillis));
    editor.putString("off",String.valueOf(endTimeMillis));
    editor.putInt("count", count);
    editor.commit();

    Intent intent =new Intent(MainActivity.this, Vactivity.class);
    startActivity(intent);

    }

}

Vactivity类(MainActivity退出时启动):

public class Vactivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    String onValue = preferences.getString("on", "");
    String offValue = preferences.getString("off", "");
    int no = preferences.getInt("count", 0);

    DataBaseHandler db = new DataBaseHandler(this);
    db.addDetails(new PassValues(onValue, offValue, no));

    setContentView(R.layout.activity_v);
}

}

PassValues类:

public class PassValues {

String a;
String b;
int c;

public PassValues(String onValue, String offValue, int no)
{
    this.a=onValue;
    this.b=offValue;
    this.c=no;
}


public String get_onValue()
{
    return this.a;
}

public void set_onValue(String onValue)
{
    this.a=onValue;
}

public String get_offValue()
{
    return this.b;
}

public void set_offValue(String offValue)
{
    this.b=offValue;
}

public int get_count()
{
    return this.c;
}

public void set_count(int no)
{
    this.c=no;
}

}

DataBaseHandler类(处理数据库):

public class DataBaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "Analysis_DB";
private static final String TABLE_TODAY = "Analysis_for_Today";

private static final String ID = "_ID";
private static final String ON = "Activated";
private static final String OFF = "Terminated";
private static final String COUNT = "Times_Dozed";


public DataBaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "("
            + ID + " INTEGER PRIMARY KEY," + ON + " TEXT,"
            + OFF + " TEXT," + COUNT + " INTEGER," +")";
    db.execSQL(CREATE_CONTACTS_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_TODAY);

    // Create tables again
    onCreate(db);

}


public void addDetails(PassValues p)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(ID, 1);
    values.put(ON, p.get_onValue());
    values.put(OFF, p.get_offValue());
    values.put(COUNT, p.get_count());

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

}

}

LogCat的部分:

    04-12 07:54:46.197: E/SQLiteLog(2604): (1) table Analysis_for_Today has no column named Activated
    04-12 07:54:46.287: E/SQLiteDatabase(2604): Error inserting Activated=1397303677511 Terminated=1397303685201 Times_Dozed=1 _ID=1
    04-12 07:54:46.287: E/SQLiteDatabase(2604): android.database.sqlite.SQLiteException: table Analysis_for_Today has no column named Activated (code 1): , while compiling: INSERT INTO Analysis_for_Today(Activated,Terminated,Times_Dozed,_ID) VALUES (?,?,?,?)

1 个答案:

答案 0 :(得分:1)

您的问题出现在CREATE TABLE SQL Command

+ OFF + " TEXT," + COUNT + " INTEGER," +")";  // remove semi column after INTEGER

使用以下

更正您的CREATE TABLE SQL Command
 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "("
        + ID + " INTEGER PRIMARY KEY," + ON + " TEXT,"
        + OFF + " TEXT," + COUNT + " INTEGER" +")";