2016-12-02 84 views
0

我有4列數據庫int id |字符串數據|字符串日期| int啓動,我有一些數據。我有方法getRow(字符串s)當我用字符串調用它的id或數據和更改查詢該選項它的工作原理,但是當我試圖獲得相同的日期行它不能通過cursor.moveToFirst條件。查詢不返回任何數據SQLite

這裏是我的代碼:

String CREATE_TABLE = "CREATE TABLE " 
      + TABLE_NAME + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_DATA 
      + " TEXT," + COLUMN_DATE + " TEXT," + COLUMN_BOOT + " Integer" + ")"; 


public String getRowID(String id){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where " + COLUMN_ID + " = " + id, null); 
    if (c != null && c.moveToFirst()) { 
     //loggin succes 
     return "string"; 
    }else return null; 
} 

public String getRowDate(String date){ 
    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor c = db.rawQuery("select * from " + TABLE_NAME + " where " + COLUMN_DATE + " = " + date, null); 
    if (c != null && c.moveToFirst()) { 
     //loggin succes 
     return "string"; 
    }else return null; 
} 

myDb.getRowID("1"); returning something 
myDb.getRowDate("02122016"); returning null 

我在我的數據庫中的兩個行。

1 | 0.19 | 01122016 | 0 
2 | 0.19 | 02122016 | 0 

回答

2

比較整數和字符串時要小心。你可能想知道爲什麼SQLite的會,因爲在所有的參數都是字符串進行比較整數,直到你認爲你的原始查詢看起來是這樣的:

select * from TABLE where DATE = 02122016 

這個值被解釋爲一個整數,並轉換爲文本,但它失去了過程中的前導零。您可以使用sqlite3的外殼驗證這一點:

sqlite> select 02122016; 
2122016 
sqlite> select '02122016' = 02122016; 
0 -- false 
sqlite> select cast(02122016 as text); 
2122016 

最簡單的解決方法是從DatabaseUtils引述使用方法的價值:

String escaped = DatabaseUtils.sqlEscapeString(date); 
String query = "select * from " + TABLE_NAME + " where " + COLUMN_DATE + " = " + escaped; 

一個更好的解決將是使用佔位符代替的說法。請注意,Android結合所有的參數爲字符串:

String query = "select * from " + TABLE_NAME + " where " + COLUMN_DATE + " = ?"; 
db.rawQuery(query, new String[]{date}); 

但是,我的建議是不要使用rawQuery(),而是使用真正的query()方法之一。 Here's a good example

最後,也許你應該考慮用於存儲日期的不同格式。在實踐中,我通常使用unix時間戳(從epoch開始秒或毫秒)存儲INTEGER列,或者我使用TEXT列,其值爲yyyy-MM-dd格式,因爲SQLite中的許多日期時間函數隱式支持該列。

+0

很多解決我的問題的例子和方法的很好的答案。謝謝 – Blackess

-2

這是我用來獲取數據的一些代碼。這來自developer.android.com的官方Google教程。你可以使用SQLite數據庫中的數據或類似的東西進行搜索。

import android.provider.BaseColumns; 

/** 
* Created by User on 11/3/2016. 
*/ 

public final class FeedReaderContract { 
    private FeedReaderContract(){} 

    // Inner class that defines table content 
    public static class FeedEntry implements BaseColumns{ 
     public static final String TABLE_NAME = "clickdata"; 
     public static final String COLUMN_NAME_CLICKS = "clicks"; 
     public static final String COLUMN_NAME_TIME = "time"; 
    } 
} 

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

/** 
* Created by User on 11/3/2016. 
*/ 

public class FeedReaderDbHelper extends SQLiteOpenHelper { 
    private static final String INTEGER_TYPE = " INTEGER"; 
    private static final String STRING_TYPE = " STRING"; 
    private static final String COMMA_SEP = ","; 
    private static final String SQL_CREATE_ENTRIES = 
      "CREATE TABLE " + FeedReaderContract.FeedEntry.TABLE_NAME + " (" + 
        FeedReaderContract.FeedEntry._ID + " INTEGER PRIMARY KEY," + 
        FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS + INTEGER_TYPE + COMMA_SEP + 
        FeedReaderContract.FeedEntry.COLUMN_NAME_TIME + STRING_TYPE +")"; 

    private static final String SQL_DELETE_ENTRIES = 
      "DROP TABLE IF EXISTS " + FeedReaderContract.FeedEntry.TABLE_NAME; 
    // If you change the database schema, you must increment the database version. 
    public static final int DATABASE_VERSION = 1; 
    public static final String DATABASE_NAME = "ClickData.db"; 

    public FeedReaderDbHelper(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 
    public void onCreate(SQLiteDatabase db) { 
     db.execSQL(SQL_CREATE_ENTRIES); 
    } 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // This database is only a cache for online data, so its upgrade policy is 
     // to simply to discard the data and start over 
     db.execSQL(SQL_DELETE_ENTRIES); 
     onCreate(db); 
    } 
    public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     onUpgrade(db, oldVersion, newVersion); 
    } 
} 

MainActivity:

private void saveData(int clicks, String time){ 
     FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this); 
     SQLiteDatabase db = mDbHelper.getWritableDatabase(); 
     ContentValues values = new ContentValues(); 
     values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS, clicks); 
     values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TIME, time); 
     long newRowId = db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values); 
    } 
    private void readFromFile(){ 
     FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(this); 
     SQLiteDatabase db = mDbHelper.getWritableDatabase(); 
     String[] projection = { 
       FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS, 
       FeedReaderContract.FeedEntry.COLUMN_NAME_TIME, 
     }; 
// Filter results WHERE "title" = 'My Title' 
     Cursor c = db.query(
       FeedReaderContract.FeedEntry.TABLE_NAME,      // The table to query 
       projection,        // The columns to return 
       null,        // The columns for the WHERE clause 
       null,       // The values for the WHERE clause 
       null,          // don't group the rows 
       null,          // don't filter by row groups 
       null         // The sort order 
     ); 
     c.moveToFirst(); 
     clickCount = new int[c.getCount()]; 
     int i = 0; 
     while(c.moveToNext()){ 
      int uname = c.getInt(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_CLICKS)); 
      clickCount[i] = uname; 
      i++; 
     } 
     dateCount = new String[c.getCount()]; 
     int i2 = 0; 
     while(c.moveToNext()){ 
      String uname = c.getString(c.getColumnIndexOrThrow(FeedReaderContract.FeedEntry.COLUMN_NAME_TIME)); 
      dateCount[i2] = uname; 
      i2++; 
     } 
    } 

希望這個代碼可以幫助你可以使其適應你需要做什麼。這些類在單獨的文件中,方法在MainActivity.java中。剛剛說過,當我第一次創建數據庫時,我遇到了困難,但有時您必須廢棄所有的代碼。有時候不要害怕這樣做。

+0

這不是回答問題的正確方法。 – Karakuri