Android SQLite SUM引发NullPointer
所以我正在研究一个应用程序,并且我刚刚开始学习Android的SQLite,显然我在一个问题上停留了3天。 每当我尝试运行查询以SUM月份等于特定月份的表中的字段时,我得到NullPointer抛出。
package com.example.droid;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import com.example.droid.SqDb;
import android.accounts.Account;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;
public class DbCon {
private static SQLiteDatabase database;
private SqDb SqHelper;
private String[] UserColumns =
{
SqDb.ACCOUNT_ID,
SqDb.ACCOUNT_NAME,
SqDb.ACCOUNT_USERNAME,
SqDb.ACCOUNT_PASSWORD,
SqDb.ACCOUNT_AGE,
SqDb.ACCOUNT_CITY,
SqDb.ACCOUNT_COUNTRY
};
private String [] GuestColumns =
{
SqDb.GUEST_ID,
SqDb.GUEST_NAME
};
private String [] TransColumns =
{
SqDb.TRAN_ID,
SqDb.TRAN_DESC,
SqDb.TRAN_TYPE,
SqDb.TRAN_TIME,
SqDb.TRAN_DAY,
SqDb.TRAN_MON,
SqDb.TRAN_YEAR,
SqDb.TRAN_CAT,
SqDb.TRAN_VAL
};
private static DbCon myDCon;
DbCon(Context context)
{
SqHelper = SqDb.getInstance(context);
}
public static DbCon getInstance(Context context)
{
if(myDCon == null)
{
myDCon = new DbCon(context.getApplicationContext());
}
return myDCon;
}
public void open() throws SQLException
{
database = SqHelper.getWritableDatabase();
}
public void close()
{
SqHelper.close();
}
public void createUser(String username, String password, String name, String Age, String City, String Country)
{
ContentValues values = new ContentValues();
values.put(SqDb.ACCOUNT_USERNAME, username);
values.put(SqDb.ACCOUNT_PASSWORD, password);
values.put(SqDb.ACCOUNT_NAME, name);
values.put(SqDb.ACCOUNT_AGE, Age);
values.put(SqDb.ACCOUNT_CITY, City);
values.put(SqDb.ACCOUNT_COUNTRY, Country);
database.insert(SqDb.ACCOUNT, null, values);
}
public void createGuest(String name)
{
ContentValues values = new ContentValues();
values.put(SqDb.GUEST_NAME, name);
database.insert(SqDb.GUEST, null, values);
}
public void AddTask(String desc, String type, String cat, double Value)
{
ContentValues values = new ContentValues();
values.put(SqDb.TRAN_DESC, desc);
values.put(SqDb.TRAN_TYPE, type);
values.put(SqDb.TRAN_CAT, cat);
values.put(SqDb.TRAN_VAL, Value);
SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Calendar cal = Calendar.getInstance();
String timedata = df.format(cal.getTime());
String[] whens = timedata.split(" ");
String time = whens[1];
String[] dates = whens[0].split("/");
values.put(SqDb.TRAN_TIME, time);
values.put(SqDb.TRAN_DAY, dates[2]);
values.put(SqDb.TRAN_MON, dates[1]);
values.put(SqDb.TRAN_YEAR, dates[0]);
database.insert(SqDb.TRAN, null, values);
}
Calendar cal = Calendar.getInstance();
public int getMonthInt(){
int month = cal.get(Calendar.MONTH);
return month;
}
public double CountMonthExp()
{
double din = 0;
this.open();
Cursor c = database.rawQuery("SELECT sum("+SqDb.TRAN_VAL+") as"+SqDb.TRAN_VAL+" FROM "+SqDb.TRAN+" WHERE "+SqDb.TRAN_TYPE+"=EXPENDITURE and "+SqDb.TRAN_MON+"=3", null);
if(c.moveToFirst()){
din = c.getDouble(0);
}
else{
din = 0;
}
return din;
}
}
有什么建议么? 是的,桌子创建和工作,我测试插入记录工作正常。
这是错误:
03-19 00:18:38.119 E / AndroidRuntime(19822):致命异常:main 03-19 00:18:38.119:E / AndroidRuntime(19822):java.lang.RuntimeException:无法启动活动ComponentInfo {com。 example.droid / com.example.droid.Hist_act}:android.database.sqlite.SQLiteException:no such column:EXPENDITURE:,while compiling:SELECT sum(tran_val)astran_val FROM tran WHERE tran_type = EXPENDITURE and tran_mon = 3 03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.ActivityThread .handleLaunchActivity(ActivityThread.java:1667)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03-19 00:18:38.119: E / AndroidRuntime(19822):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.os.Handler.dispatchMessage(Handler .java:99)03-19 00:18:38.119:E / Andro idRuntime(19822):at android.os.Looper.loop(Looper.java:130)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.ActivityThread.main(ActivityThread.java:3687 )03-19 00:18:38.119:E / AndroidRuntime(19822):at java.lang.reflect.Method.invokeNative(Native Method)03-19 00:18:38.119:E / AndroidRuntime(19822):at java。 lang.reflect.Method.invoke(Method.java:507)03-19 00:18:38.119:E / AndroidRuntime(19822):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867 )03-19 00:18:38.119:E / AndroidRuntime(19822):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)03-19 00:18:38.119:E / AndroidRuntime(19822 ):at dalvik.system.NativeStart.main(Native Method)03-19 00:18:38.119:E / AndroidRuntime(19822):引起:android.database.sqlite.SQLiteException:没有这样的列:EXPENDITURE:,编译时:SELECT sum(tran_val)astran_val FROM tran WHERE tran_type = EXPENDITURE and tran_mon = 3 03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQ LiteCompiledSql.native_compile(Native Method)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:65)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteProgram。(SQLiteProgram。 java:83)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:49)03-19 00:18:38.119:E / AndroidRuntime(19822 ):at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java: 1356)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324)03-19 00:18:38.119:E / AndroidRuntime(19822) :at com.example.droid.DbCon.CountMonthExp(DbCon.java:122)0 3-19 00:18:38.119:E / AndroidRuntime(19822):at com.example.droid.Hist_act.onCreate(Hist_act.java:42)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)03-19 00:18:38.119:E / AndroidRuntime(19822):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)03-19 00: 18:38.119:E / AndroidRuntime(19822):... 11更多
我会说这里至少有两个问题:
SELECT sum(tran_val) astran_val FROM tran WHERE tran_type=EXPENDITURE and tran_mon=3
我怀疑你的意思是:
SELECT sum(tran_val) as tran_val FROM tran WHERE tran_type='EXPENDITURE' and tran_mon=3
我相信你将能够自己改变代码来实现这些调整。
链接地址: http://www.djcxy.com/p/96815.html上一篇: Android SQLite SUM, throws NullPointer
下一篇: Google Maps Android v2 not working with ActionBarSherlock Fragment Activity