2015-11-04 65 views
0

我有兩個表,類別和交易。我有單個SQLiteOpenHelper類和兩個類來插入和檢索類別和事務。但是我無法在事務表中插入。插入時獲取負數。無法插入到第二個表(交易)在android sqlite

DatabaseHelper類

package com.mm.bipin.mm; 

import android.content.Context; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DatabaseHelper extends SQLiteOpenHelper { 

private static final String DATABASE_NAME="mm"; 
private static final int DATABASE_VERSION=1; 

public static final String CATEGORY_TABLE_NAME="category"; 
public static final String CATEGORY_CID="_id"; 
public static final String CATEGORY_TITLE= "title"; 
public static final String CATEGORY_DATE="date"; 
public static final String CATEGORY_TYPE="type"; 
public static final String CATEGORY_NOTE="note"; 
private static final String CATEGORY_CREATE_TABLE= "CREATE TABLE "+CATEGORY_TABLE_NAME+" ("+CATEGORY_CID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+ 
     " "+CATEGORY_TITLE+" VARCHAR(255), "+CATEGORY_DATE+" VARCHAR(255),"+CATEGORY_TYPE+" INTEGER,"+CATEGORY_NOTE+" VARCHAR(255))"; 
private static final String CATEGORY_DROP_TABLE= 
     " DROP TABLE IF EXISTS " +CATEGORY_TABLE_NAME; 


public static final String TRANSACTION_TABLE_NAME="transaction"; 
public static final String TRANSACTION_TID="_id"; 
public static final String TRANSACTION_CATEGORY_ID="category_id"; 
public static final String TRANSACTION_TITLE="title"; 
public static final String TRANSACTION_AMOUNT="amount"; 
public static final String TRANSACTION_DATE="date"; 
public static final String TRANSACTION_NOTE="note"; 


private static final String TRANSACTION_CREATE_TABLE= "CREATE TABLE "+TRANSACTION_TABLE_NAME+" " + 
     " ("+TRANSACTION_TID+" INTEGER PRIMARY KEY AUTOINCREMENT, " + 
     " "+TRANSACTION_CATEGORY_ID+" INTEGER , " + 
     " "+TRANSACTION_TITLE+" VARCHAR(255) , " + 
     " "+TRANSACTION_AMOUNT+" VARCHAR(255)  , " + 
     " "+TRANSACTION_DATE+" VARCHAR(255) , " + 
     " "+TRANSACTION_NOTE+" VARCHAR(255)) "; 

private static final String TRANSACTION_DROP_TABLE= 
     " DROP TABLE IF EXISTS " +TRANSACTION_TABLE_NAME; 

private Context context; 
public DatabaseHelper(Context context) { 
    super(context, DATABASE_NAME,null,DATABASE_VERSION); 
    this.context=context; 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
    try { 
     //getWritableDatabase(); 
     db.execSQL(CATEGORY_CREATE_TABLE); 
     db.execSQL(TRANSACTION_CREATE_TABLE); 
     Message.message(context, "DB created"); 
    } 
    catch(android.database.SQLException e){ 
     Message.message(context," "+e); 
    } 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    try{ 
     db.execSQL(CATEGORY_DROP_TABLE); 
     db.execSQL(TRANSACTION_DROP_TABLE); 
     Message.message(context,"DB dropped"); 
    } 
    catch(android.database.SQLException e){ 
     Message.message(context," "+e); 
    } 
} 
} 

CategoryDatabaseAdapter類

package com.mm.bipin.mm.category; 

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.widget.Toast; 

import com.mm.bipin.mm.Category; 
import com.mm.bipin.mm.DatabaseHelper; 
import com.mm.bipin.mm.Message; 

import java.util.ArrayList; 


public class CategoryDatabaseAdapter { 
DatabaseHelper helper; 
SQLiteDatabase db; 
public static String[] columns={DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE,DatabaseHelper.CATEGORY_NOTE}; 
public CategoryDatabaseAdapter(Context context){ 
    helper=new DatabaseHelper(context); 
} 
Context context; 
public long insertCategory(String title,int year,int month,int day,int type,String note){ 
    db=helper.getWritableDatabase(); 
    ContentValues contentValues=new ContentValues(); 
    contentValues.put(DatabaseHelper.CATEGORY_TITLE,title); 
    String date= convertDateToString(year, month, day); 
    contentValues.put(DatabaseHelper.CATEGORY_DATE,date); 
    contentValues.put(DatabaseHelper.CATEGORY_TYPE,type); 
    contentValues.put(DatabaseHelper.CATEGORY_NOTE,note); 
    long id=db.insert(DatabaseHelper.CATEGORY_TABLE_NAME,null,contentValues); 

    return id; 
} 



public Cursor getAllRows(){ 
    db=helper.getWritableDatabase(); 
    String[] columns={DatabaseHelper.CATEGORY_CID,DatabaseHelper.CATEGORY_TITLE,DatabaseHelper.CATEGORY_DATE, 
      DatabaseHelper.CATEGORY_TYPE,DatabaseHelper.CATEGORY_NOTE}; 
    Cursor mCursor=db.query(DatabaseHelper.CATEGORY_TABLE_NAME,columns,null,null,null,null,null); 
    if(mCursor!=null){ 
     mCursor.moveToNext(); 
    } 

    return mCursor; 

} 

public String convertDateToString(int year,int month,int day){ 
    StringBuilder str=new StringBuilder(); 
    str.append(year); 
    str.append("-"); 
    str.append(month); 
    str.append("-"); 
    str.append(day); 
    return str.toString(); 
} 
} 

TransactionDatabaseAdapter類

package com.mm.bipin.mm.transaction; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.DatabaseUtils; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.widget.Toast; 

import com.mm.bipin.mm.DatabaseHelper; 
import com.mm.bipin.mm.Message; 
import com.mm.bipin.mm.category.CategoryDatabaseAdapter; 


public class TransactionDatabaseAdapter { 
DatabaseHelper helper; 
SQLiteDatabase db; 

public static String[] columns= {DatabaseHelper.TRANSACTION_TITLE,DatabaseHelper.TRANSACTION_DATE, 
    DatabaseHelper.TRANSACTION_NOTE}; 
TransactionDatabaseAdapter(Context context){ 
    helper=new DatabaseHelper(context); 
} 

public long insertTransaction(String title,int category_id ,String amount,int year,int month,int day,String note){ 
    //SQLiteDatabase db=helper.getWritableDatabase(); 

    db=helper.getWritableDatabase(); 
    ContentValues contentValues=new ContentValues(); 
    contentValues.put(DatabaseHelper.TRANSACTION_CATEGORY_ID,category_id); 
    contentValues.put(DatabaseHelper.TRANSACTION_TITLE,title); 
    contentValues.put(DatabaseHelper.TRANSACTION_AMOUNT,amount); 

    String date= convertDateToString(year, month, day); 
    contentValues.put(DatabaseHelper.TRANSACTION_DATE,date); 
    contentValues.put(DatabaseHelper.TRANSACTION_NOTE,note); 
    long id=db.insert(DatabaseHelper.TRANSACTION_TABLE_NAME,null,contentValues); 
    return id; 
} 
public String convertDateToString(int year,int month,int day){ 
    StringBuilder str=new StringBuilder(); 
    str.append(year); 
    str.append("-"); 
    str.append(month); 
    str.append("-"); 
    str.append(day); 
    return str.toString(); 
} 
public Cursor getAllTransaction(){ 
    db=helper.getWritableDatabase(); 
    String[] transactionColumns={DatabaseHelper.TRANSACTION_TID,DatabaseHelper.TRANSACTION_CATEGORY_ID, DatabaseHelper.TRANSACTION_TITLE, 
      DatabaseHelper.TRANSACTION_AMOUNT,DatabaseHelper.TRANSACTION_DATE,DatabaseHelper.TRANSACTION_NOTE}; 
    Cursor mCursor=db.query(DatabaseHelper.TRANSACTION_TABLE_NAME,transactionColumns,null,null,null,null,null); 
    if(mCursor!=null) mCursor.moveToNext(); 

    return mCursor; 
} 
} 

這些都是我的三類數據庫功能。 insertTransaction()返回負值,這意味着數據沒有被插入。

由於openhelper衝突可能存在問題。但我不知道如何以及在哪裏。任何幫助將不勝感激。謝謝!

+0

使用'insertOrThrow'獲取錯誤消息。 –

+0

我發現我在創建表'transaction'時遇到了SQLException e中的錯誤。錯誤語句是:android.database.sqlite.SQLException:附近「事務」:語法錯誤(代碼1):,編譯時:CREATE TABLE事務(_id INTEGER PRIMARY KEY AUTOINCREMENT,category_id INTEGER,title VARCHAR(255),amount VARCHAR 255),日期VARCHAR(255),注意VARCHAR(255)) –

回答

0

我自己找到了答案。我創建了表名「transaction」,它恰好是sqlite關鍵字。我應該注意sql關鍵字。現在,我將表名改爲「交易」,現在正在工作。 Thankyou。