SQLite 数据库查询和 AsyncTask

Posted

技术标签:

【中文标题】SQLite 数据库查询和 AsyncTask【英文标题】:SQLite Database query and AsyncTask 【发布时间】:2017-03-22 14:38:33 【问题描述】:

你好安卓专家!!!

我目前正在编写一个具有 SQLite 数据库和 MPcharts 的应用程序。这个想法是我创建了一个带有几个数据库表的 SQLite 数据库,并将数据从数据库中提取出来并创建 MP 图表。我在不同的应用程序中使用了类似的方法,效果很好。在这种情况下,查询稍微复杂一些,应用程序停止,我的图表上没有显示任何数据。在网上调查之后,我觉得 AsyncTask 可以帮助我解决这个问题。我不确定如何将 AsyncTask 应用于我的案例。基本上,将 Arraylist xDataMonthCategory() 和 Arraylist yDataPieChartCategoryMonth() 链接到 Piechart 并使用 AsyncTask 在后台运行 SQLite 查询。我测试了我的查询,我知道数据在那里。请帮我。非常感谢!!!

这是 MP Piechart Java 类:

        import android.graphics.Color;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;

    import com.github.mikephil.charting.charts.PieChart;
    import com.github.mikephil.charting.components.Legend;
    import com.github.mikephil.charting.data.Entry;
    import com.github.mikephil.charting.data.PieData;
    import com.github.mikephil.charting.data.PieDataSet;
    import com.github.mikephil.charting.formatter.PercentFormatter;

    import java.util.ArrayList;

public class MTDPieCharts extends AppCompatActivity 

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mtdpie_charts);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() 
        @Override
        public void onClick(View view) 
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        
    );
    MTDPieChartCategory();
    //MTDPieChartItems();


//MTD Category PieChart
public void MTDPieChartCategory() 

    DataBaseHelper databasehelper = new DataBaseHelper(MTDPieCharts.this);
    PieChart MTDcategoryChart = new PieChart(this);
    MTDcategoryChart = (PieChart) findViewById(R.id.MTDPieChartCategory);
    //get yVals and datasets of MTD Category chart
    ArrayList<Entry> yValsCategoryAMT = new ArrayList<Entry>();
    for(int i = 0; i < databasehelper.yDataPieChartCategoryMonth().size(); i++)
        yValsCategoryAMT.add(new Entry(databasehelper.yDataPieChartCategoryMonth().get(i), i));

    //get xVals of MTD Category chart
    ArrayList<String> xValsCategory = new ArrayList<String>();
    for(int i = 0; i < databasehelper.xDataMonthCategory().size(); i++)
        xValsCategory.add(databasehelper.xDataMonthCategory().get(i));

    PieDataSet MTDCategorySet = new PieDataSet(yValsCategoryAMT, "");

    //set yvals attribute
    MTDCategorySet.setSliceSpace(3);
    MTDCategorySet.setSelectionShift(5);

    //add many colors
    MTDCategorySet.setColors(new int[]R.color.BlueA100, R.color.Cyan500, R.color.colorAccent, R.color.DeepPurpleA200, R.color.BlueA100, R.color.Teal200, R.color.Pink200, R.color.Pink500, R.color.purple,this);

    // configue pie chart
    MTDcategoryChart.setUsePercentValues(true);
    MTDcategoryChart.setRotationEnabled(true);
    MTDcategoryChart.setRotationAngle(0);
    MTDcategoryChart.setDescriptionPosition(350, 550);
    MTDcategoryChart.setDrawHoleEnabled(true);
    MTDcategoryChart.setHoleColor(Color.TRANSPARENT);
    MTDcategoryChart.setHoleRadius(13);
    MTDcategoryChart.setTransparentCircleRadius(18);
    MTDcategoryChart.getLegend().setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
    //MTDcategoryChart.getLegend().setXEntrySpace(3);
    // MTDcategoryChart.getLegend().setYEntrySpace(3);
    MTDcategoryChart.getLegend().setTextColor(Color.BLUE);
    MTDcategoryChart.setDescription("Category");
    MTDcategoryChart.setDescriptionTextSize(13);
    MTDcategoryChart.setDescriptionColor(Color.BLUE);

    //get category chart data
    PieData MTDCategoryPieData = new PieData(xValsCategory, MTDCategorySet);
    MTDCategoryPieData.setValueFormatter(new PercentFormatter());
    MTDCategoryPieData.setValueTextSize(11);
    MTDCategoryPieData.setValueTextColor(Color.BLUE);
    MTDcategoryChart.setData(MTDCategoryPieData);


    //refresh data
    MTDcategoryChart.invalidate();


数据库助手类:

    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.util.Log;

    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;

      //create category table
public static final String Create_Table_Category="create table if not exists "+
        TableData.TableInfo.DataBase_Table_Category+  "(" +
        TableData.TableInfo.COL_C1_CategoryID+" integer primary key autoincrement,"+
        TableData.TableInfo.COL_C2_Description+" text not null);";

        //create table spending
        public static final String Create_Table_Spending="create table if not exists "+
        TableData.TableInfo.DataBase_Table_Spending+" ("+TableData.TableInfo.COL_S1_SpendingID+" integer primary key autoincrement,"+
        TableData.TableInfo.COL_S2_Spending_DT+" Datetime not null,"+
        TableData.TableInfo.COL_S3_ItemID+" integer not null,"+
        TableData.TableInfo.COL_S4_AMT+" Float not null," +
        "FOREIGN key ("+TableData.TableInfo.COL_S3_ItemID+") REFERENCES " +TableData.TableInfo.DataBase_Table_Items+" ("+TableData.TableInfo.COL_I1_ItemID+") ON DELETE CASCADE);";

// create table Item

public static final String Create_Table_Items="Create table if not exists " +
        TableData.TableInfo.DataBase_Table_Items+" ("+
        TableData.TableInfo.COL_I1_ItemID+" integer primary key autoincrement,"+
        TableData.TableInfo.COL_I2_Description+" Text not null,"+
        TableData.TableInfo.COL_I3_CategoryID+" integer not null,"+

        "FOREIGN key ("+TableData.TableInfo.COL_I3_CategoryID+") REFERENCES " +TableData.TableInfo.DataBase_Table_Category+ " ("+TableData.TableInfo.COL_C1_CategoryID+") ON DELETE CASCADE);";
   // create table Users
public static final String Created_Table_Users="create table if not exists "+
       TableData.TableInfo.DataBase_Table_Users+ " ("+ TableData.TableInfo.COL_U1_UserID+" integer primary key autoincrement ,"+
       TableData.TableInfo.COL_U2_UserName+" text not null ,"+ TableData.TableInfo.COL_U3_Password+" Text not null, "+
       TableData.TableInfo.COL_U4_Created_DT+" Datetime not null);";

public static final String Drop_table_Category="drop table if exists "+TableData.TableInfo.DataBase_Table_Category;
public static final String Drop_table_Items="drop table if exists "+TableData.TableInfo.DataBase_Table_Items;
public static final String Drop_table_Spending="drop table if exists "+TableData.TableInfo.DataBase_Table_Spending;
public static final String Drop_table_Users="drop table if exists "+ TableData.TableInfo.DataBase_Table_Users;

SQLiteDatabase db;
// declare variables
public static final String YearEndPieChartMonth="YearEndPieChartMonth";
public static final YearEndPieChartMonthValues=  String                    "YearEndPieChartMonthValues";
public static final String MonthPieChartCategory="MonthPieChartCategory";
public static final String MonthPieCHartCategoryValues="MonthPieCHartCategoryValues";
public static final String MonthPieChartItems="MonthPieChartItems";
public static final String MonthPiechartItemsValues="MonthPiechartItemsValues";
public static final String YearEndPieChartCategory="YearEndPieChartCategory";
public static final String YearEndCategoryValues="YearEndCategoryValues";
public static final String YearEndPieChartItems="YearEndPieChartItems";
public static final String YearEndItemsValues="YearEndItemsValues";

public DataBaseHelper(Context context) 
    super(context, TableData.TableInfo.DataBase_Name, null, TableData.TableInfo.DataBase_version);




@Override
public void onCreate(SQLiteDatabase db) 
    //create Category table
    db.execSQL(Create_Table_Category);
    Log.d("Database operation:", "Category table created");
    //insert data into Category table
    String sql_category="INSERT INTO " +       TableData.TableInfo.DataBase_Table_Category+" ("+     TableData.TableInfo.COL_C2_Description+" ) 
  VALUES('Housing'),('Utilities'),('Household'),('Groceries'),('Living'),('Transportation'),('HealthCare'),('Personal'),('EatingOut'),('Entertainments'),('Children'),('DebtPayments'),('Savings')";
    db.execSQL(sql_category);
    Log.d("Category table:","Data inserted");

    //create Items table
    db.execSQL(Create_Table_Items);
    Log.d("Datbase operation:", "Item table created ");
    // insert data into Item table
    String sql_Items="insert into "+ TableData.TableInfo.DataBase_Table_Items+ " ("+ TableData.TableInfo.COL_I2_Description+" ,"+ TableData.TableInfo.COL_I3_CategoryID+" )  values('Rent',1),('Mortgage',1),('StrataFees',1),('PropertyTaxes',1),('HouseInsurance',1),('Phone',2),('Cell',2),('CableTVInternet',2),('GAS',2),('Hybro',2), ('Security',2),('Electricity',2),('Water',2),('StorageLocker',3),('Decor',3),('Gardening',3),('Garbage',3),('MiscItems',3),('Food',4),('BabyNeeds',4),('Cleaning',4),('Toiletries',4),('Kitchenwares',4),('PersonalCare',5),('BankFees',5), ('DryCleaning',5),('PetCosts',5),('Fuel',6),('AutoInsurance',6),('Transit',6),('Parking',6),('Taxi',6),('MedicalPremiums',7),('LifeInsurance',7),('Medication',7),('EyeCare',7),('Dental',7),('WellnessCosts',7),('Tobacco',8),('Alcohol',8),('BooksCDs',8),('Donations',8),('Subscriptions',8),('Meals',9),('Snacks',9),('TakeOut',9),('CoffeeWater',9),('SportsEquipmentFees',10),('Movies',10),('Hobbies',10),('Gaming',10),('FitnessMemberships',10),('DayCare',11),('LessonsActivities',11),('Allowance',11),('SchoolSuppliesFees',11),('Babysitting',11),('Loans',12),('CreditCards',12),('Leases',12),('SupportPayments',12),('GovernmentDebts',12),('EmergencyFund',13),('RRSP',13),('RESPs',13),('TravelVacations',13),('CarRepairs',13),('CLothing',13), ('Gifts',13),('AssistingFamilyFriends',13)";
    db.execSQL(sql_Items);
    Log.d("Items","Data inserted");
    //create spending table
    db.execSQL(Create_Table_Spending);
    Log.d("Database operation:", "Spending table created ");
    db.execSQL(Created_Table_Users);


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
    db.execSQL(Drop_table_Category);
    db.execSQL(Drop_table_Items);
    db.execSQL(Drop_table_Spending);
    db.execSQL(Drop_table_Users);
    onCreate(db);


public DataBaseHelper open() throws SQLException 
    db = this.getWritableDatabase();
    return this;


public void close() 
    db.close();


public SQLiteDatabase getDatabaseInstance() 
    return db;


public String getTime()
    SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    Date date=new Date();

    return dateFormat.format(date);



 //current month pie chart category
public ArrayList<String> xDataMonthCategory()
    ArrayList<String> xnewDataCategory=new ArrayList<String>();
    SQLiteDatabase db=this.getReadableDatabase();
    String sql="select distinct c."+ TableData.TableInfo.COL_C2_Description+" as MonthPieChartCategory from " + TableData.TableInfo.DataBase_Table_Spending+" s join "+ TableData.TableInfo.DataBase_Table_Items+" i on i."+ TableData.TableInfo.COL_I1_ItemID+"=s."+ TableData.TableInfo.COL_S3_ItemID+" join "+ TableData.TableInfo.DataBase_Table_Category+" c on c."+ TableData.TableInfo.COL_C1_CategoryID+"=i."+ TableData.TableInfo.COL_I3_CategoryID+" where strftime('%Y',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%Y',date('now')) and strftime('%m',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%m',date('now')) group by  c."+ TableData.TableInfo.COL_C2_Description+" order by  c."+ TableData.TableInfo.COL_C2_Description+" desc";
    Cursor cursor=db.rawQuery(sql,null);
    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToLast()) 
        xnewDataCategory.add(cursor.getString(cursor.getColumnIndex(MonthPieChartCategory)));
    
    cursor.close();
    return xnewDataCategory;


//current month pie chart category y values
public ArrayList<Float> yDataPieChartCategoryMonth()
    ArrayList<Float> ynewDataPieCHartCategoryMonth=new ArrayList<Float>();
    SQLiteDatabase db=this.getReadableDatabase();
    String sql="select sum("+ TableData.TableInfo.COL_S4_AMT+") MonthPieCHartCategoryValues,c."+ TableData.TableInfo.COL_C2_Description+" Category from "+ TableData.TableInfo.DataBase_Table_Spending+" s join "+ TableData.TableInfo.DataBase_Table_Items+" i on i."+ TableData.TableInfo.COL_I1_ItemID+"=s."+ TableData.TableInfo.COL_S3_ItemID+ " join "+ TableData.TableInfo.DataBase_Table_Category+" c on c."+ TableData.TableInfo.COL_C1_CategoryID+"=i."+ TableData.TableInfo.COL_I3_CategoryID + " where strftime('%Y',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%Y',Date('now')) and strftime('%m',"+ TableData.TableInfo.COL_S2_Spending_DT+")=strftime('%m',date('now')) group by c."+ TableData.TableInfo.COL_C2_Description+" order by Category  desc";
    Cursor cursor=db.rawQuery(sql,null);
    for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToLast())
        ynewDataPieCHartCategoryMonth.add(cursor.getFloat(cursor.getColumnIndex(MonthPieCHartCategoryValues)));
    
    cursor.close();
    return ynewDataPieCHartCategoryMonth;


【问题讨论】:

【参考方案1】:

您必须使用光标加载器和内容提供程序。

https://developer.android.com/training/load-data-background/index.html

【讨论】:

好吧,您没有必须使用光标加载器和内容提供程序,但这并不是一个糟糕的方法。您确实必须在后台线程上进行数据库访问,而不是在 UI 线程上。有很多方法可以做到这一点。 你能给我一个提示吗?我正在尝试做....但还不能。我是新手。 我能做到这一点,但不知道下一步该去哪里。

以上是关于SQLite 数据库查询和 AsyncTask的主要内容,如果未能解决你的问题,请参考以下文章

SQlite数据库操作和AsyncTask [重复]

Android AsyncTask 和 SQLite 数据库实例

在 AsyncTask 上尝试使用 SQLite 检索数据的奇怪行为

如果我在 AsyncTask 线程中插入数据,如何从 UI 线程访问 SQLite 数据库?

在 Android 上使用 SQLite 时如何避免并发问题?

Android PushNotification IntentService 并调用 AsyncTask 函数从服务器数据库获取数据并更新 sqlite 数据库