组织数据库Android

时间:2015-05-02 20:57:52

标签: android database sqlite

我有一个包含以下条目的android数据库: 名称,日期,位置,开始时间,结束时间,总时间。

这是我的数据库代码:

public class Database extends Activity {

// Used For Logging Database Version Changes//
private static final String TAG = "Database";

// Row Names//
public static final String KEY_ROW_ID_NUMBER = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_DATE = "date";
public static final String KEY_LOCATION = "location";
public static final String KEY_STARTTIME = "starttime";
public static final String KEY_ENDTIME = "endtime";
public static final String KEY_TOTALTIME = "totaltime";
public static final String KEY_TOTALTIME_ADDED = "totaltime_added";
public static final String KEY_DATE_EVENT_ADDED = "date_event_added";
public static final String KEY_SIGNATURE = "signature";

// Put All Rows Into A String//
public static final String[] ALL_KEYS = new String[] {KEY_ROW_ID_NUMBER, KEY_NAME, KEY_DATE, KEY_LOCATION, KEY_STARTTIME, KEY_ENDTIME, KEY_TOTALTIME, KEY_TOTALTIME_ADDED, KEY_DATE_EVENT_ADDED};
public static final String[] CSV_EXPORT = new String[] {KEY_NAME, KEY_DATE, KEY_LOCATION, KEY_STARTTIME, KEY_ENDTIME, KEY_TOTALTIME, KEY_SIGNATURE};

// Column Numbers For Each Row Name:
public static final int COL_NAME = 1;
public static final int COL_DATE = 2;
public static final int COL_LOCATION = 3;
public static final int COL_STARTTIME = 4;
public static final int COL_ENDTIME = 5;
public static final int COL_TOTALTIME = 6;
public static final int COL_TOTALTIME_ADDED = 7;

// DataBase info:
public static final String DATABASE_NAME = "community_service_Database";
public static final String DATABASE_TABLE = "events";
public static final int DATABASE_VERSION = 2; // The version number must be incremented each time a change to DB structure occurs.

//SQL Statement To Create Database//
private static final String DATABASE_CREATE_SQL
        = "CREATE TABLE " + DATABASE_TABLE
        + " (" + KEY_ROW_ID_NUMBER + " INTEGER PRIMARY KEY AUTOINCREMENT, "
        + KEY_NAME + " TEXT NOT NULL,"
        + KEY_DATE + " TEXT NOT NULL,"
        + KEY_LOCATION + " TEXT NOT NULL,"
        + KEY_STARTTIME + " TEXT NOT NULL,"
        + KEY_ENDTIME + " TEXT NOT NULL,"
        + KEY_TOTALTIME + " TEXT NOT NULL,"
        + KEY_TOTALTIME_ADDED + " TEXT NOT NULL,"
        + KEY_DATE_EVENT_ADDED + " TEXT NOT NULL,"
        + KEY_SIGNATURE + " TEXT NOT NULL"
        + ");";

// Define Variable DatabaseHelper db_helper//
public static DatabaseHelper db_helper;

// Define Variable SQLiteDatabase db//
public static SQLiteDatabase db;

// Integer TotalTimeAdded/
public static int TotalTimeAdded;

// Call Upon Database Helper//
public Database(Context ctx) {

    // Link DB_Helper To Database_Helper//
    db_helper = new DatabaseHelper(ctx);
}

// Open The Database Connection//
public Database open() {

    // Call DatabaseHelper To Open Database//
    db = db_helper.getWritableDatabase();
    return this;
}

// Close The Database Connection//
public void close() {

    // Calls DatabaseHelper To Close Database//
    db_helper.close();
}

// Add A New Set Of Values To Be Inserted Into The Database//
public long insertRow (String name, String date, String location, String starttime, String endtime, String totaltime, String totaltime_added, String signature, String date_event_added) {

    // Gets All The New Values//
    ContentValues initialValues = new ContentValues();

    // ALl New Values Being Added//
    initialValues.put(KEY_NAME, name);
    initialValues.put(KEY_DATE, date);
    initialValues.put(KEY_LOCATION, location);
    initialValues.put(KEY_STARTTIME, starttime);
    initialValues.put(KEY_ENDTIME, endtime);
    initialValues.put(KEY_TOTALTIME, totaltime);
    initialValues.put(KEY_TOTALTIME_ADDED, totaltime_added);
    initialValues.put(KEY_SIGNATURE, signature);
    initialValues.put(KEY_DATE_EVENT_ADDED, date_event_added);

    // Inserts The Value Data Into The Database//
    return db.insert(DATABASE_TABLE, null, initialValues);
}

// Change an existing row to be equal to new data.
public boolean updateRow(String rowId, String name, String date, String location, String starttime, String endtime, String totaltime, String totaltime_added, String signature, String date_event_added) {

    // Get Current Row By ID Number//
    String where = KEY_ROW_ID_NUMBER + "=" + rowId;

    // Gets New Values For Row//
    ContentValues newValues = new ContentValues();

    // Add New Values
    newValues.put(KEY_NAME, name);
    newValues.put(KEY_NAME, name);
    newValues.put(KEY_DATE, date);
    newValues.put(KEY_LOCATION, location);
    newValues.put(KEY_STARTTIME, starttime);
    newValues.put(KEY_ENDTIME, endtime);
    newValues.put(KEY_TOTALTIME, totaltime);
    newValues.put(KEY_TOTALTIME_ADDED, totaltime_added);
    newValues.put(KEY_SIGNATURE, signature);
    newValues.put(KEY_DATE_EVENT_ADDED, date_event_added);

    // Inserts The New Value Data Into The Database//
    return db.update(DATABASE_TABLE, newValues, where, null) != 0;
}

// Delete a row from the database, by rowId (primary key)
public boolean deleteRow(long rowId) {

    // Get Specific Row ID//
    String where = KEY_ROW_ID_NUMBER + "=" + rowId;

    // Delete Row//
    return db.delete(DATABASE_TABLE, where, null) != 0;
}

// Delete Everything From Database//
public void deleteAll() {

    // Get All Rows//
    Cursor c = getAllRows();

    // Delete Row One By One//
    long rowId = c.getColumnIndexOrThrow(KEY_ROW_ID_NUMBER);
    if (c.moveToFirst()) {
        do {
            deleteRow(c.getLong((int) rowId));              
        } while (c.moveToNext());
    }
    c.close();
}

// Return all data in the database.
public Cursor getAllRows() {
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, null, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get a specific row (by rowId)
public Cursor getRow(String rowId) {
    String where = KEY_ROW_ID_NUMBER + "=" + rowId;
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, 
                    where, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    return c;
}

// Get All Community Service Times//
public Cursor total_time_added() {

    // Set Total Time Added To Zero//
    TotalTimeAdded = 0;

    // Query Through All Rows//
    Cursor c =  db.query(true, DATABASE_TABLE, ALL_KEYS, null, null, null, null, null, null);
    if (c != null) {
        c.moveToFirst();
    }
    assert c != null;
    while (!c.isAfterLast()) {

        // Get Total Time Added//
        TotalTimeAdded  = TotalTimeAdded + c.getInt(COL_TOTALTIME_ADDED);
        c.moveToNext();
    }

    return c;
}

// Helps Make Database Work (Don't Touch)//
private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase _db) {
        _db.execSQL(DATABASE_CREATE_SQL);           
    }

    @Override
    public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading application's database from version " + oldVersion
                + " to " + newVersion + ", which will destroy all old data!");

        // Destroy old database:
        _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

        // Recreate new database:
        onCreate(_db);
    }
  }
}

是否有人知道使用我所拥有的字段组织数据库的方法,例如按名称,日期或位置进行组织?谢谢。组织的意思是我希望数据库按名称排序,例如代替添加行的日期。

1 个答案:

答案 0 :(得分:0)

在数据库中存储行是一个实现细节。你不应该担心它们的存储方式。相反,您应该担心如何按特定顺序检索它们。要获取表的所有行,您将使用类似SELECT * FROM my_table_name的查询,这将列出表的所有行。要按特定顺序对它们进行排序,您只需要在查询结束时指定它,如SELECT * FROM my_table_name ORDER BY name(其中name是列的名称),您可以使用DESC关键字反转订单,该关键字代表&# 34;降序&#34 ;. SELECT * FROM my_table_name ORDER BY name DESC

您询问的是如何在存储数据后以特定方式检索数据。我建议你查看关于SQL的w3schools教程。以下是ORDER BY关键字http://www.w3schools.com/sql/sql_orderby.asp

上的页面

编辑:我还想提一下其他的东西。只是从那里查看代码,我可以看到你有很多评论。限制代码中的注释数量通常是一个好主意。例如,你有这个......

// Open The Database Connection//
public Database open() {

但是这里看到这个评论是多余的,因为你的代码告诉你数据库正在打开。不要重复自己。