gpt4 book ai didi

java - 当我尝试向数据库插入一行时,发生了 ,"table timeline has no column named source"”

转载 作者:太空宇宙 更新时间:2023-11-04 14:03:39 24 4
gpt4 key购买 nike

我确实有一个名为 source 的专栏。我不知道我错在哪里。我在 onCreate 方法中构造了表时间。

public class DbHelper1 extends SQLiteOpenHelper {
static final String TAG = "DbHelper1";
static final String DB_NAME = "timeline.db";
static final int DB_VERSION = 1;
static final String TABLE = "timeline";
static final String C_ID = BaseColumns._ID;
static final String C_CREATED_AT = "created_at";
static final String C_SOURCE = "source";//C_SOURCE represents source
static final String C_TEXT = "txt";
static final String C_USER = "user";
Context context;

public DbHelper1(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
StringBuffer buffer = new StringBuffer();
buffer.append("create table " + TABLE + " (");
buffer.append(C_ID + " int primary key ,");
buffer.append(C_CREATED_AT + " int ,");
buffer.append(C_USER + " text ,");
buffer.append(C_TEXT + " text ,");
buffer.append(C_SOURCE+" text ");//here is my column source
buffer.append(")");
String sql=buffer.toString();
db.execSQL(sql);
Log.d(TAG, "onCreate sql :"+sql);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table if exists "+TABLE);
Log.d(TAG, "onUpgrade");
onCreate(db);
}
}

这是我的测试服务:

public class UpdaterService3 extends Service {
private static final String TAG = "UpdaterService3";
static final int DELAY = 3000;
private boolean runFlag = false;
private Updater updater;
private YambaApplication1 yamba;

DbHelper1 dbHelper;
SQLiteDatabase db;

@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}

@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
yamba = (YambaApplication1) getApplication();
this.updater = new Updater();
dbHelper = new DbHelper1(this);
Log.d(TAG, "onCreate");
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO Auto-generated method stub
super.onStartCommand(intent, flags, startId);
this.runFlag = true;
this.updater.start();
this.yamba.setServiceRunning(true);
Log.d(TAG, "onStarted");
return START_STICKY;
}

@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
this.runFlag = false;
this.updater.interrupt();
this.updater = null;
this.yamba.setServiceRunning(false);
Log.d(TAG, "onDestroy");
}

private class Updater extends Thread {
public Updater() {
super("UpdaterService-Updater");
}

@Override
public void run() {
// TODO Auto-generated method stub
UpdaterService3 updaterService = UpdaterService3.this;
while (updaterService.runFlag) {
Status status;
Log.d(TAG, "Updater running");
try {
db=dbHelper.getWritableDatabase();
Log.d(TAG, "Updater ran");
status = new Status();
status.createdAt = String.valueOf(System
.currentTimeMillis());
status.id = UUID.randomUUID().toString();
status.source = "hello i'm " + status.id
+ ",nice to see you";
status.text = "wow baby :" + status.id;
status.user = "user:" + status.id;
ContentValues values = new ContentValues();
values.clear();
values.put(DbHelper1.C_ID, status.id);
values.put(DbHelper1.C_CREATED_AT, status.createdAt);
values.put(DbHelper1.C_SOURCE, status.source);
values.put(DbHelper1.C_TEXT, status.text);
values.put(DbHelper1.C_USER, status.user);
db.insertOrThrow(DbHelper1.TABLE, null, values);
Log.d(TAG, String.format("%s:%s", status.user,status.text));
db.close();
Log.d(TAG, "Updater run");
Thread.sleep(DELAY);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
updaterService.runFlag = false;
}
}
}
}
}

这里是类(class)状态

public class Status {//entity bean
String id;
String createdAt;
String source;
String text;
String user;
}

我真的很困惑。我是 android 的新编​​码员。请帮忙!~

最佳答案

1 DB_VERSION 必须命名为 DATABASE_VERSION。它不是一个可选常量。如果您及时修改数据库结构,Android 就会依赖此常量来执行升级。

2如果您更改了数据库结构(即:添加或重命名列),则必须增加 DATABASE_VERSION 常量值,以便触发 onUpgrade() 方法。

关于java - 当我尝试向数据库插入一行时,发生了 ,"table timeline has no column named source"”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29074107/

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