gpt4 book ai didi

android - 没有列错误 : SQLite Android

转载 作者:行者123 更新时间:2023-11-30 02:57:34 24 4
gpt4 key购买 nike

即使此应用程序没有错误,但每次调用 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);
}

传递值类:

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 (?,?,?,?)

最佳答案

您的问题出在您的CREATE TABLE SQL 命令

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

使用以下内容更正您的 CREATE TABLE SQL 命令

 String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_TODAY + "("
+ ID + " INTEGER PRIMARY KEY," + ON + " TEXT,"
+ OFF + " TEXT," + COUNT + " INTEGER" +")";

关于android - 没有列错误 : SQLite Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23030382/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com