我正在尝试在我的Android应用程序中创建一个SQLite数据库,但是当我创建数据库时,应用程序意外停止工作。
package com.example.expenditurelogs;
import android.app.Activity;
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;
import android.text.format.Time;
import android.widget.TextView;
public class CTable extends Activity{
public static final String KEY_SNO = "SNo.";
public static final String KEY_ITEM = "Item";
public static final String KEY_PRICE = "Price";
public static final String KEY_TIME = "Time";
private static final String DATABASE_NAME = "CExpenditureLogs";
private static final String DATABASE_CTABLE = "CanteenTable";
private static final int DATABASE_VERSION = 1;
private CDBHelper cdb;
private final Context con;
private SQLiteDatabase canteendb;
private static class CDBHelper extends SQLiteOpenHelper
{
public CDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+ DATABASE_CTABLE+ " ("+KEY_SNO+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ KEY_ITEM + " TEXT NOT NULL, "+ KEY_PRICE+
" TEXT NOT NULL, "+KEY_TIME+" TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+DATABASE_CTABLE);
onCreate(db);
}
}
public CTable(Context c)
{
con = c;
}
public CTable open() throws SQLException
{
cdb = new CDBHelper(con);
canteendb = cdb.getWritableDatabase();
return this;
}
public void close()
{
cdb.close();
}
public long createEntry(String item,String price)
{
ContentValues cv = new ContentValues();
cv.put(KEY_ITEM,item);
cv.put(KEY_PRICE,price);
Time t = new Time(Time.getCurrentTimezone());
t.setToNow();
String time = ""+t.month;
cv.put(KEY_TIME,time);
return canteendb.insert(DATABASE_CTABLE,null,cv);
}
public void getData()
{
TextView sno,item,price,time;
sno = (TextView) findViewById(R.id.tSno);
item = (TextView) findViewById(R.id.tItem);
price = (TextView) findViewById(R.id.tPrice);
time = (TextView) findViewById(R.id.tDateandTime);
sno.setText("");
item.setText("");
price.setText("");
time.setText("");
String[] columns = { KEY_SNO,KEY_ITEM,KEY_PRICE,KEY_TIME };
Cursor c = canteendb.query(DATABASE_CTABLE,columns,null,null,null,null,null);
int iSno = c.getColumnIndex(KEY_SNO);
int iItem = c.getColumnIndex(KEY_ITEM);
int iPrice = c.getColumnIndex(KEY_PRICE);
int iTime = c.getColumnIndex(KEY_TIME);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
sno.setText(sno+ c.getString(iSno) +"."+"\n");
item.setText(item+c.getString(iItem)+"\n");
price.setText(price+c.getString(iPrice)+"\n");
time.setText(time+c.getString(iTime)+"\n");
}
}
}
答案 0 :(得分:0)
SNo.
不是有效的列名。
为了能够在标识符中使用特殊字符,您必须引用它:
public static final String KEY_SNO = "\"SNo.\"";
但最好不要使用句号:
public static final String KEY_SNO = "SNo";