我在将数据和时间插入sq-lite数据库时遇到错误,当我在表格中插入日期时间列时出现错误。
package com.example.moneymanager;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Lending extends Activity implements OnClickListener{
Button sqlAdd;
EditText sqlName, sqlAmount, sqlRate, sqlTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.lending);
sqlAdd = (Button) findViewById(R.id.bAdd);
sqlName = (EditText) findViewById(R.id.etName);
sqlAmount = (EditText) findViewById(R.id.etAmount);
sqlRate = (EditText) findViewById(R.id.etRate);
sqlTime = (EditText) findViewById(R.id.etTime);
sqlAdd.setOnClickListener(this);
} //oncreate
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean didItWork = true;
try{
String name = sqlName.getText().toString();
String amount = sqlAmount.getText().toString();
String rate = sqlRate.getText().toString();
String time = sqlTime.getText().toString();
java.util.Date dt = new java.util.Date();
java.text.SimpleDateFormat sdf =
new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String currentTime = sdf.format(dt);
LendDatabase entry = new LendDatabase(Lending.this);
entry.open();
entry.createEntry(name, amount, rate, time, currentTime);
entry.close();
}catch (Exception e){
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang it!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Heck Yea!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
}//onclick
}
package com.example.moneymanager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class LendDatabase {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_AMOUNT = "persons_amount";
public static final String KEY_RATE = "persons_rate"; //rate of interest
public static final String KEY_TIME = "persons_time"; //time in months for which money is lend
public static final String KEY_TIMEST = "time_of_lending"; //current date and time
private static final String DATABASE_NAME = "Lendingdb1";
private static final String DATABASE_TABLE = "peopleTable";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
/*arg0.execSQL("DROP TABLE " + DATABASE_TABLE );*/
arg0.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_AMOUNT + " INTEGER NOT NULL, " +
KEY_RATE + " INTEGER NOT NULL, " +
KEY_TIME + " INTEGER NOT NULL, " +
KEY_TIMEST + "DATETIME NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
arg0.execSQL("DROP_TABLE_IF_EXISTS " + DATABASE_TABLE);
onCreate(arg0);
}
}
public LendDatabase(Context c){
ourContext = c;
}
public LendDatabase open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String amount, String rate,
String time, String currentTime) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_AMOUNT, amount);
cv.put(KEY_RATE, rate);
cv.put(KEY_TIME, time);
cv.put(KEY_TIMEST, currentTime);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{ KEY_ROWID, KEY_NAME, KEY_AMOUNT, KEY_RATE, KEY_TIME, KEY_TIMEST};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iAmount = c.getColumnIndex(KEY_AMOUNT);
int iRate = c.getColumnIndex(KEY_RATE);
int iTime = c.getColumnIndex(KEY_TIME);
int iTimest = c.getColumnIndex(KEY_TIMEST);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result = result + c.getString(iRow) + ".Name :" + c.getString(iName) + "\n Amount :" + c.getString(iAmount) + "\n Rate :" + c.getString(iRate) + "\n Time :" + c.getString(iTime) + "\n Time :" + c.getString(iTimest) + "\n";
}
return result;
}
}
这是logcat:
07-28 21:33:50.983: W/KeyCharacterMap(274): No keyboard for id 0
07-28 21:33:50.994: W/KeyCharacterMap(274): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
07-28 21:34:12.384: I/Database(274): sqlite returned: error code = 1, msg = table peopleTable has no column named time_of_lending
07-28 21:34:12.443: E/Database(274): Error inserting time_of_lending=2014-07-28 21:34:12 persons_rate=665 persons_amount=2131 persons_time=4 persons_name=asdf
07-28 21:34:12.443: E/Database(274): android.database.sqlite.SQLiteException: table peopleTable has no column named time_of_lending: , while compiling: INSERT INTO peopleTable(time_of_lending, persons_rate, persons_amount, persons_time, persons_name) VALUES(?, ?, ?, ?, ?);
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
07-28 21:34:12.443: E/Database(274): at com.example.moneymanager.LendDatabase.createEntry(LendDatabase.java:81)
07-28 21:34:12.443: E/Database(274): at com.example.moneymanager.Lending.onClick(Lending.java:49)
07-28 21:34:12.443: E/Database(274): at android.view.View.performClick(View.java:2408)
07-28 21:34:12.443: E/Database(274): at android.view.View$PerformClick.run(View.java:8816)
07-28 21:34:12.443: E/Database(274): at android.os.Handler.handleCallback(Handler.java:587)
07-28 21:34:12.443: E/Database(274): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 21:34:12.443: E/Database(274): at android.os.Looper.loop(Looper.java:123)
07-28 21:34:12.443: E/Database(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 21:34:12.443: E/Database(274): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 21:34:12.443: E/Database(274): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 21:34:12.443: E/Database(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 21:34:12.443: E/Database(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 21:34:12.443: E/Database(274): at dalvik.system.NativeStart.main(Native Method)
07-28 21:34:12.443: E/Database(274): Error inserting time_of_lending=2014-07-28 21:34:12 persons_rate=665 persons_amount=2131 persons_time=4 persons_name=asdf
07-28 21:34:12.443: E/Database(274): android.database.sqlite.SQLiteException: table peopleTable has no column named time_of_lending: , while compiling: INSERT INTO peopleTable(time_of_lending, persons_rate, persons_amount, persons_time, persons_name) VALUES(?, ?, ?, ?, ?);
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
07-28 21:34:12.443: E/Database(274): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
07-28 21:34:12.443: E/Database(274): at com.example.moneymanager.LendDatabase.createEntry(LendDatabase.java:81)
07-28 21:34:12.443: E/Database(274): at com.example.moneymanager.Lending.onClick(Lending.java:49)
07-28 21:34:12.443: E/Database(274): at android.view.View.performClick(View.java:2408)
07-28 21:34:12.443: E/Database(274): at android.view.View$PerformClick.run(View.java:8816)
07-28 21:34:12.443: E/Database(274): at android.os.Handler.handleCallback(Handler.java:587)
07-28 21:34:12.443: E/Database(274): at android.os.Handler.dispatchMessage(Handler.java:92)
07-28 21:34:12.443: E/Database(274): at android.os.Looper.loop(Looper.java:123)
07-28 21:34:12.443: E/Database(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 21:34:12.443: E/Database(274): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 21:34:12.443: E/Database(274): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 21:34:12.443: E/Database(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 21:34:12.443: E/Database(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 21:34:12.443: E/Database(274): at dalvik.system.NativeStart.main(Native Method)
07-28 21:34:20.873: I/Database(274): sqlite returned: error code = 1, msg = no such column: time_of_lending
07-28 21:34:20.873: D/AndroidRuntime(274): Shutting down VM
07-28 21:34:20.883: W/dalvikvm(274): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-28 21:34:20.913: E/AndroidRuntime(274): FATAL EXCEPTION: main
07-28 21:34:20.913: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.moneymanager/com.example.moneymanager.LendingView}: android.database.sqlite.SQLiteException: no such column: time_of_lending: , while compiling: SELECT _id, persons_name, persons_amount, persons_rate, persons_time, time_of_lending FROM peopleTable
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 21:34:20.913: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 21:34:20.913: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 21:34:20.913: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-28 21:34:20.913: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-28 21:34:20.913: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
07-28 21:34:20.913: E/AndroidRuntime(274): Caused by: android.database.sqlite.SQLiteException: no such column: time_of_lending: , while compiling: SELECT _id, persons_name, persons_amount, persons_rate, persons_time, time_of_lending FROM peopleTable
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1345)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1229)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1184)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1264)
07-28 21:34:20.913: E/AndroidRuntime(274): at com.example.moneymanager.LendDatabase.getData(LendDatabase.java:88)
07-28 21:34:20.913: E/AndroidRuntime(274): at com.example.moneymanager.LendingView.onCreate(LendingView.java:17)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 21:34:20.913: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 21:34:20.913: E/AndroidRuntime(274): ... 11 more
07-28 21:34:26.283: I/Process(274): Sending signal. PID: 274 SIG: 9
我在create table命令中有日期时间列,但后来我也收到了错误。
答案 0 :(得分:2)
您在create语句中缺少KEY_TIMEST
及其数据类型之间的空格:
arg0.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_AMOUNT + " INTEGER NOT NULL, " +
KEY_RATE + " INTEGER NOT NULL, " +
KEY_TIME + " INTEGER NOT NULL, " +
KEY_TIMEST + " DATETIME NOT NULL);" // space added here
);
答案 1 :(得分:0)
arg0.execSQL("CREATE TABLE " + DATABASE_TABLE + "(" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_AMOUNT + " INTEGER NOT NULL, " +
KEY_RATE + " INTEGER NOT NULL, " +
KEY_TIME + " INTEGER NOT NULL, " +
KEY_TIMEST + " created_at DATETIME DEFAULT CURRENT_TIMESTAMP);" //this way works fine
适用于Long和String值,使用字符串我得到完整的格式化日期,而使用long我得到一个很长的数字,这对于稍后操作日期非常有用
答案 2 :(得分:-2)
您在数据库中将时间列定义为INTEGER并且它没有问题,因为时间可以存储为INTEGER或TEXT,但是当您尝试插入新行时,您会给它一个字符串。也许将时间转换为Long来解决您的问题。试一试
Date d = new Date(); //gives you the current time.
Long newTime = d.getTime();
cv.put(KEY_TIME, newTime);