我试图通过组合其他三个整数字段来更新我的SQLite数据库中的整数字段,但如果字段值的长度为1,我还需要将其他两个字段的前缀加上零。
这是我尝试通过DBAdapter类中的升级数据库方法执行的例程。
所以我需要更改当前的更新:
if (newVersion==2) {
populatesql = "update " + DB_TABLE +
" set field1="+ COL_FIELD2 +
" || " +
COL_FIELD3 +
" || " +
COL_FIELD4;
}
到这样的东西(是的我知道前缀不会起作用,因为它需要String不是整数但是我用String尝试了它并使它静止但是它不起作用)
if (newVersion==2) {
populatesql = "update " + DB_TABLE +
" set field1="+ COL_FIELD2 +
" || " +
prefixzero(COL_FIELD3) +
" || " +
prefixzero(COL_FIELD4);
}
public String prefixzero(String number) {
String result = Integer.toString(number);
// Log.d("PREFIX", "result starts with this = "+result);
if (result.length() >1 ) {
// Log.d("PREFIX", "NO ZEROES HERE");
return Integer.toString(number);
}
String zeroprefix = "";
zeroprefix = "0"+result;
// Log.d("PREFIX", zeroprefix);
return zeroprefix ;
}
数据库字段声明如下:
public static final String COL_FIELD4 = "day";
public static final String COL_FIELD3 = "month";
public static final String COL_FIELD2 = "year";
public static final String COL_FIELD1 = "long_date";
这是我遗漏空间的错误。没有空格的想法就是字段名称简单地连接在一起。使用的代码低于此错误。
02-28 07:34:00.919: E/AndroidRuntime(18332): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.common.myapp/co.uk.common.myapp.MainActivity}: android.database.sqlite.SQLiteException: no such column: yearmonthday: update Log set long_date= yearmonthday
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1658)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1674)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:942)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.os.Handler.dispatchMessage(Handler.java:99)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.os.Looper.loop(Looper.java:130)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread.main(ActivityThread.java:3735)
02-28 07:34:00.919: E/AndroidRuntime(18332): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 07:34:00.919: E/AndroidRuntime(18332): at java.lang.reflect.Method.invoke(Method.java:507)
02-28 07:34:00.919: E/AndroidRuntime(18332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
02-28 07:34:00.919: E/AndroidRuntime(18332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:662)
02-28 07:34:00.919: E/AndroidRuntime(18332): at dalvik.system.NativeStart.main(Native Method)
02-28 07:34:00.919: E/AndroidRuntime(18332): Caused by: android.database.sqlite.SQLiteException: no such column: yearmonthday: update Log set long_date= yearmonthday
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1763)
02-28 07:34:00.919: E/AndroidRuntime(18332): at co.uk.shieldstothemax.blastedneighbours.DBAdapter$DBHelper.onUpgrade(DBAdapter.java:68)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
02-28 07:34:00.919: E/AndroidRuntime(18332): at co.uk.shieldstothemax.blastedneighbours.DBAdapter.open(DBAdapter.java:87)
02-28 07:34:00.919: E/AndroidRuntime(18332): at co.uk.shieldstothemax.blastedneighbours.MainActivity.onCreate(MainActivity.java:123)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-28 07:34:00.919: E/AndroidRuntime(18332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
onUpgrade方法中的Cod:
if (newVersion==2) {
populatesql = "update "+ DB_TABLE +" set long_date= "+getDateStr(COL_FIELD2,COL_FIELD3,COL_FIELD4);
}
getDateStr:
public static String getDateStr(String year, String month,String day) {
return year+ prefixfieldzero(month)+ prefixfieldzero(day);
}
prefixfieldzero:
public static String prefixfieldzero(String number) {
String result = String.valueOf(number);
// Log.d("PREFIX", "result starts with this = "+result);
if (result.length() >1 ) {
// Log.d("PREFIX", "NO ZEROES HERE");
return number;
}
String zerofieldprefix = "";
zerofieldprefix = "0"+result;
// Log.d("PREFIX", zeroprefix);
return zerofieldprefix ;
}
这是我的DBAdapter:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
private static final String DB_NAME = "TABLE";
private static final String DB_TABLE = "Log";
private static final int DB_VERSION = 2;
private static final String DB_CREATE = "CREATE TABLE IF NOT EXISTS "+DB_TABLE+
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"type_id INTEGER, " +" hour INTEGER, " +
"min INTEGER , " +" info VARCHAR,"+"image_desc VARCHAR, "+
"day INTEGER, "+"month INTEGER, "+ "year INTEGER,"+" second INTEGER,long_date INTEGER ,date_time_long INTEGER NOT NULL "+");" ;
public static final String COL_TYPE = "type_id";
public static final String COL_HOUR = "hour";
public static final String COL_MIN = "min";
public static final String COL_SEC = "second";
public static final String COL_IMAGE = "image_desc";
public static final String COL_DAY = "day";
public static final String COL_MON = "month";
public static final String COL_YEAR = "year";
public static final String COL_DATE = "date";
public static final String COL_LONG_DATE = "long_date";
public static final String COL_INFO = "info";
public static final String COL_ID = "_id";
private SQLiteDatabase mDB;
private DBHelper mDBHelper;
private Context mCtx;
private static class DBHelper extends SQLiteOpenHelper{
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DB_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.v("OLDDBVERSION", "Old version="+String.valueOf(oldVersion));
Log.v("NEWDBVERSION", "New version="+String.valueOf(newVersion));
//String upgradesql = null;
String populatesql = null;
if (newVersion==2) {
populatesql = "update "+ DB_TABLE +" set long_date= "+getDateStr(COL_YEAR,COL_MON,COL_DAY);
}
if (populatesql !=null) {
//db.execSQL(upgradesql);
if (populatesql !=null) {
db.execSQL(populatesql);
}
}
}
}
public DBAdapter(Context ctx)
{
this.mCtx = ctx;
}
//OPEN METHOD AND CLOSE METHOD
public DBAdapter open()
{
mDBHelper = new DBHelper(mCtx);
mDB = mDBHelper.getWritableDatabase(); //important
return this;
}
public void close()
{
mDBHelper.close();
}
//INSERT AND DELETE
public long createLog(String info,String image, Integer type,Integer hour,Integer mins,Integer sec,Integer day,Integer month,Integer year,Long longdate,Long date)
{
ContentValues v = new ContentValues();
v.put(COL_TYPE, type);
v.put(COL_INFO, info);
v.put(COL_IMAGE, image);
v.put(COL_HOUR, hour);
v.put(COL_SEC, sec);
v.put(COL_MIN, mins);
v.put(COL_DAY, day);
v.put(COL_MON, month);
v.put(COL_YEAR, year);
v.put(COL_DATE, date);
v.put(COL_LONG_DATE, longdate);
return mDB.insert(DB_TABLE, null, v);
}
public boolean deleteLogRec(long id)
{
return mDB.delete(DB_TABLE, COL_ID + "="+ id, null)>0;
}
public boolean deleteAllLogs()
{
return mDB.delete(DB_TABLE, null, null)>0;
}
//get all bookmark
public Cursor GetAllLogs(Integer i,String sortfield)
{
String sorted = "";
if (i == 1 ) {
sorted = "DESC";
} else if (i == 2) {
sorted = "ASC";
}
return mDB.query(DB_TABLE, new String[] {COL_ID, COL_TYPE,COL_IMAGE, COL_INFO,COL_IMAGE,COL_HOUR,COL_SEC,COL_MIN,COL_DAY,COL_MON,COL_YEAR,COL_SORT_DATE},
null, null, null, null, COL_DATE+" "+sorted);
}
public Cursor allrecords() {
return mDB.query(DB_TABLE, new String[] {COL_ID, COL_TYPE,COL_IMAGE, COL_INFO,COL_IMAGE,COL_HOUR,COL_SEC,COL_MIN,COL_DAY,COL_MON,COL_YEAR,COL_SORT_DATE},
null, null, null, null, null);
}
//get a specific log by id
public Cursor getLog(long id)
{
Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {COL_ID, COL_TYPE,COL_IMAGE, COL_INFO,COL_IMAGE,COL_HOUR,COL_SEC,COL_MIN,COL_DAY,COL_MON,COL_YEAR},
COL_ID + "=" + id,
null, null, null, null, null);
if (mCursor!=null)
mCursor.moveToFirst();
return mCursor;
}
//finally update the log
public boolean updateLog(long id, String uinfo, String uimage,Integer utype,Integer uhour,Integer usec,Integer umin,Integer uday,Integer umonth,Integer uyear,Long ulongdate,Long udate)
{
ContentValues v = new ContentValues();
v.put(COL_TYPE, utype);
v.put(COL_INFO, uinfo);
v.put(COL_IMAGE, uimage);
v.put(COL_HOUR, uhour);
v.put(COL_SEC, usec);
v.put(COL_MIN, umin);
v.put(COL_DAY, uday);
v.put(COL_MON, umonth);
v.put(COL_YEAR, uyear);
v.put(COL_DATE, udate);
v.put(COL_SORT_DATE, ulongdate);
return mDB.update(DB_TABLE, v, COL_ID + "=" + id, null) > 0;
}
public static String prefixfieldzero(String number) {
String result = String.valueOf(number);
// Log.d("PREFIX", "result starts with this = "+result);
if (result.length() >1 ) {
// Log.d("PREFIX", "NO ZEROES HERE");
return number;
}
String zerofieldprefix = "";
zerofieldprefix = "0"+result;
// Log.d("PREFIX", zeroprefix);
return zerofieldprefix ;
}
public static String getDateStr(String year, String month,String day) {
return year+ prefixfieldzero(month)+ prefixfieldzero(day);
}
}
尝试按照您的最新答案运行SQL,但收到此错误:
03-01 00:20:05.379: E/AndroidRuntime(19060): Caused by: android.database.sqlite.SQLiteException: near "AS": syntax error: update TABLE AS TABLE_Temp1 set long_date = (select year || substr('00' || month ,-2,2 ) || substr('00' || ,day, -2 ,2) from Log AS TABLE_Temp2 where TABLE_Temp1.id=TABLE_Temp2.id
当前SQL用于将长日期更新为年月和日的组合:
populatesql = "update "+DB_TABLE + " AS LogTemp1 set long_date = (select year || substr('00' || month ,-2,2 ) || substr('00' || ,day, -2 ,2) from "+DB_TABLE+" AS LogTemp2 where LogTemp1.id=LogTemp2.id " ;
我也尝试过如下:
populatesql = "update "+DB_TABLE + " AS LogTemp1 set long_date = (select year || substr('00' || month ,-2,2 ) || substr('00' || ,day, -2 ,2) from "+DB_TABLE+" AS LogTemp2 where **LogTemp1._id=LogTemp2._id** " ;
答案 0 :(得分:1)
好的,我终于得到了,你想要的。您需要一个更新语句,根据其当前值更新每一行。当然,您无法从SQLite数据库调用Java代码中的函数。因此,一切都必须在SQLite本身的UPDATE语句中完成。
鉴于此,您的陈述应如下所示:
UPDATE yourTable AS table1
SET date =
(SELECT year ||
substr('00' || month, -2, 2) ||
substr('00' || day, -2, 2)
FROM yourTable AS table2
WHERE table1.id = table2.id);
您只需将我在此处使用的名称替换为您实际使用的名称,并为其创建有效的Java字符串。
substr('00' || month, -2, 2)
语句执行其他数据库引擎的lpad
语句(在SQLite中不可用)会执行的操作。
你可以这样读:用一个数字连接两个零,并取最终字符串中最右边的两个字符。 -2
表示从右边开始子字符串计数,2
表示将两个字符作为结果子字符串。
此lpad
替换基于a blogpost Jason Hinkle。
答案 1 :(得分:0)
假设COL_FIELD2 ...是字符串值,为什么不这样做呢?
if (newVersion==2) {
populatesql = "update " + DB_TABLE +
" set field1 = " + getDateStr(COL_FIELD2, COL_FIELD3, COL_FIELD4);
}
getDateStr(String year, String month,String day) {
return year + prefixzero(month) + prefixzero(day);
}
public String prefixzero(String number) {
//Log.d("PREFIX", "result starts with this = "+result);
if (number.length() > 1) {
//Log.d("PREFIX", "NO ZEROES HERE");
return number;
}
number = "0" + number;
//Log.d("PREFIX", zeroprefix);
return zeroprefix ;
}
如果它们是int
- 值,您可以使用以下prefixzero()
方法(当然也会调整getDateStr()
的方法签名:
public String prefixzero(int number) {
if (number > 9) {
return Integer.toString(number);
}
return "0" + number;
}