2016-12-02 86 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