首先,您应该创建一个类来创建 DB Like :
package com.DB;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.webkit.WebChromeClient.CustomViewCallback;
public class DatabaseHandler_Dashboard extends SQLiteOpenHelper {
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "dashboard";
// Contacts table name
private static final String TABLE_NAME = "tbl_dashboard";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String RANK = "RANK";
private static final String MHBHUBCD = "MHBHUBCD";
private static final String WK1Dkts = "WK1Dkts";
private static final String WK1Per = "WK1Per";
private static final String WK1OnTime = "WK1OnTime";
private static final String WK2Dkts = "WK2Dkts";
private static final String Wk2Per = "Wk2Per";
private static final String WK2OnTime = "WK2OnTime";
private static final String QTDDkts = "QTDDkts";
private static final String QTDPer = "QTDPer";
private static final String QTDOnTime = "QTDOnTime";
private static final String YTDDkts = "YTDDkts";
private static final String YTDPer = "YTDPer";
private static final String YTDOnTime = "YTDOnTime";
public DatabaseHandler_Dashboard(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CREATE_DEVICE_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ RANK + " TEXT,"
+ MHBHUBCD + " TEXT,"
+ WK1Dkts + " TEXT,"
+ WK1Per + " TEXT,"
+ WK1OnTime + " TEXT,"
+ WK2Dkts + " TEXT,"
+ Wk2Per + " TEXT,"
+ WK2OnTime + " TEXT,"
+ QTDDkts + " TEXT,"
+ QTDPer + " TEXT,"
+ QTDOnTime + " TEXT,"
+ YTDDkts + " TEXT,"
+ YTDPer + " TEXT,"
+ YTDOnTime + " TEXT"
+ ")";
db.execSQL(CREATE_DEVICE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
/**
* All CRUD(Create, Read, Update, Delete) Operations
*/
// Adding new Device
public void addDevice(Contact_Dashboard device) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(RANK , device.get_RANK());
values.put(MHBHUBCD , device.get_MHBHUBCD ());
values.put(WK1Dkts , device.get_WK1Dkts());
values.put(WK1Per , device.get_WK1Per());
values.put(WK1OnTime , device.get_WK1OnTime());
values.put(WK2Dkts , device.get_WK2Dkts());
values.put(Wk2Per , device.get_Wk2Per());
values.put(WK2OnTime , device.get_WK2OnTime());
values.put(QTDDkts , device.get_QTDDkts());
values.put(QTDPer , device.get_QTDPer());
values.put(QTDOnTime , device.get_QTDOnTime());
values.put(YTDDkts , device.get_YTDDkts());
values.put(YTDPer , device.get_YTDPer());
values.put(YTDOnTime , device.get_YTDOnTime());
// Inserting Row
db.insert(TABLE_NAME, null, values);
db.close(); // Closing database connection
}
// Getting All Devices
public List<Contact_Dashboard> getAllDevice() {
List<Contact_Dashboard> deviceList = new ArrayList<Contact_Dashboard>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor !=null)
{
if (cursor.moveToFirst()) {
do {
Contact_Dashboard device = new Contact_Dashboard();
device.set_RANK(cursor.getString(0));
device.set_MHBHUBCD(cursor.getString(1));
device.set_WK1Dkts(cursor.getString(2));
device.set_WK1Per (cursor.getString(3));
device.set_WK1OnTime (cursor.getString(4));
device.set_WK2Dkts (cursor.getString(5));
device.set_Wk2Per (cursor.getString(6));
device.set_WK2OnTime (cursor.getString(7));
device.set_QTDDkts (cursor.getString(8));
device.set_QTDPer (cursor.getString(9));
device.set_QTDOnTime (cursor.getString(10));
device.set_YTDDkts (cursor.getString(11));
device.set_YTDPer (cursor.getString(12));
device.set_YTDOnTime (cursor.getString(13));
// Adding Device to list
deviceList.add(device);
} while (cursor.moveToNext());
}
}
cursor.close();
return deviceList;
}
// Getting contacts Count
public int get_all_data() {
String countQuery = "SELECT * FROM " + TABLE_NAME;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(countQuery, null);
cursor.close();
// return count
return cursor.getCount();
}
public int del_all_data()
{
SQLiteDatabase db = this.getWritableDatabase();
return db.delete(TABLE_NAME, null, null);
}
// Getting All Devices
public List<Contact_Dashboard> getAllDevice_sort_by(String vall,String sort_type)
{
List<Contact_Dashboard> deviceList = new ArrayList<Contact_Dashboard>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " order by CAST(" + vall + " as integer)" + sort_type;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor !=null)
{
if (cursor.moveToFirst()) {
do {
Contact_Dashboard device = new Contact_Dashboard();
device.set_RANK(cursor.getString(0));
device.set_MHBHUBCD(cursor.getString(1));
device.set_WK1Dkts(cursor.getString(2));
device.set_WK1Per (cursor.getString(3));
device.set_WK1OnTime (cursor.getString(4));
device.set_WK2Dkts (cursor.getString(5));
device.set_Wk2Per (cursor.getString(6));
device.set_WK2OnTime (cursor.getString(7));
device.set_QTDDkts (cursor.getString(8));
device.set_QTDPer (cursor.getString(9));
device.set_QTDOnTime (cursor.getString(10));
device.set_YTDDkts (cursor.getString(11));
device.set_YTDPer (cursor.getString(12));
device.set_YTDOnTime (cursor.getString(13));
// Adding Device to list
deviceList.add(device);
} while (cursor.moveToNext());
}
}
cursor.close();
return deviceList;
}
}
现在在您的其他活动页面中创建此类的对象,例如:
DatabaseHandler_Dashboard ECDS_DB = new DatabaseHandler_Dashboard(this);
因此,当该活动运行时,它会自动生成您的数据库并检查数据库是否存在......
现在您可以为变量创建一个 get set 方法并在每个活动中使用 DB 类对象,例如:
ECDS_DB.del_all_data();
ECDS_DB.addDevice(new Contact_Dashboard(
ARR_RANK[i] ,
ARR_MHBHUBCD[i] ,
ARR_WK1Dkts[i] ,
ARR_WK1OnTime[i] ,
ARR_WK1Per[i] ,
ARR_WK2Dkts[i] ,
ARR_WK2OnTime[i] ,
ARR_Wk2Per[i] ,
ARR_QTDDkts[i] ,
ARR_QTDOnTime[i] ,
ARR_QTDPer[i] ,
ARR_YTDDkts[i] ,
ARR_YTDOnTime[i] ,
ARR_YTDPer[i] ));
试试这个,别忘了给我评论......