你好,我是Android的新手,所以我按照教程使用sqlite。
我有两个表类别和收入,我这两个表(相同的功能)的处理方式相同,但我很惊讶结果与它们不一样,表Categories
工作正常,但对于Income
,错误消息为SQLLiteLog (1) no such table: Income
这是我的数据库类
public class CategoriesDAO extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "ischeduletable";
// Contacts table name
private static final String TABLE_CONTACTS = "Categories";
private static final String TABLE_INCOME = "Income";
// Contacts Table Columns names
private static final String KEY_ID = "idcateg";
private static final String KEY_NAME = "namecateg";
// Income Table Columns names
private static final String KEY_INCOME = "idincome";
private static final String KEY_MONTHINCOME = "monthlyincome";
private static final String KEY_ADDITIONINCOME = "additionalincome";
private static final String KEY_CURRENCY = "currency";
private static final String KEY_DATE = "month";
public CategoriesDAO(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT"+")";
Log.v("taggg", "i'm on createe");
Log.d("SSSSSSSSSS", "ON CREEEEEAAAAAATE");
String CREATE_TABLE_Income = "CREATE TABLE " + TABLE_INCOME + "("
+ KEY_INCOME + " INTEGER PRIMARY KEY," + KEY_MONTHINCOME + " FLOAT,"+ KEY_ADDITIONINCOME + " FLOAT," + KEY_CURRENCY + " INTEGER," + KEY_DATE+ " TEXT"+")";
db.execSQL(CREATE_CONTACTS_TABLE);
db.execSQL(CREATE_TABLE_Income);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_INCOME);
// Create tables again
onCreate(db);
}
public void addContact(CategorieCl categ) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, categ.getNamecateg()); // Contact Name
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}
public CategorieCl getCateg(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_CONTACTS, new String[] { KEY_ID,
KEY_NAME}, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
CategorieCl categ = new CategorieCl (Integer.parseInt(cursor.getString(0)),
cursor.getString(1));
return categ;
}
public int updateContact(CategorieCl cat) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, cat.getNamecateg());
// updating row
return db.update(TABLE_CONTACTS, values, KEY_ID + " = ?",
new String[] { String.valueOf(cat.getIdcateg()) });
}
public void deleteContact(CategorieCl cat) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_CONTACTS, KEY_ID + " = ?",
new String[] { String.valueOf(cat.getIdcateg()) });
db.close();
}
//////////////////////////////INCOME DATABASEHELPER///////////////////////////////////////////////////////////////////////////////
public void addIncome(IncomeCL income) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_MONTHINCOME, income.getMonthlyincome());
values.put(KEY_ADDITIONINCOME, income.getAdditionalincome());
values.put(KEY_CURRENCY, income.getIdincome());
values.put(KEY_DATE, ""+income.getMonth());
// Inserting Row
db.insert(TABLE_INCOME, null, values);
db.close(); // Closing database connection
}
public IncomeCL getincome(int id) throws ParseException {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_INCOME, new String[] { KEY_INCOME }, KEY_INCOME + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Date date = new SimpleDateFormat("ddMMyyyy").parse(cursor.getString(4));
IncomeCL income = new IncomeCL (Integer.parseInt(cursor.getString(0)),
Float.parseFloat(cursor.getString(1)), Float.parseFloat(cursor.getString(2)),Integer.parseInt(cursor.getString(3)),date);
return income;
}
}
}
这是我使用Sqlite
的课程public class Income extends Activity {
private EditText edittext;
private EditText edittext2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.income);
CategoriesDAO dab = new CategoriesDAO(this);
addKeyListener();
addKeyListener2();
Log.d("MY TAGGG ", "START SHOWING DATABASE");
Date dd = new Date();
IncomeCL ini = new IncomeCL(0, 0, 1, dd);
dab.addIncome(ini);
try {
for (int x=0;x< dab.getAllIncomes().size();x++)
{
Log.d("THIS IS THE ID", ""+dab.getAllIncomes().get(x).getIdincome());
}
} catch (ParseException e) {
Log.d("MY TAGGG ", "ERROR LODINGDATABASE");
e.printStackTrace();
}
}
如果有人告诉我该代码有什么问题,我将不胜感激!