2017-05-25 21 views
0

我已經在堆棧溢出中經歷了許多答案,但沒有任何幫助。我的應用程序通過webservices從SQL服務器獲取數據並寫入本地sqlite數據庫。我可以確認這是拉和存儲數據。我的下一步是使用分頁顯示前十行並在用戶向下滾動時加載更多。android.database.sqlite.SQLiteCantOpenDatabaseException。在一個類中工作正常,但在另一個類中拋出錯誤

我有一個代表sqlite記錄數據的類。我有一個功能createRuns()來獲取前10條記錄

公共類運行{

private int runid; 
private int id; 
private int Esky_no; 
private int Adjusted; 
private String Address; 
private int Cust_No; 
private String New; 
private String Trusted; 
private String Name; 
private String Mobile; 
private String Home; 
private String Work; 
private String PaymentType; 
private String PaymentNote; 
private String DeliveryTime; 
private String DeliveryInstructions; 
private String DriverNotes; 
private String NoDeliveryReason; 
private String IsDelivered; 

public Run() { 
} 

public Run(int runid, int id, int Esky_no, int Adjusted, String Address, int Cust_No, String New, String Trusted, String Name, String Mobile, String Home, String Work, String PaymentType, String PaymentNote, String DeliveryTime, String DeliveryInstructions, String DriverNotes, String NoDeliveryReason, String IsDelivered) { 
    this.runid = runid; 
    this.id = id; 
    this.Esky_no = Esky_no; 
    this.Adjusted = Adjusted; 
    this.Address = Address; 
    this.Cust_No = Cust_No; 
    this.New = New; 
    this.Trusted = Trusted; 
    this.Name = Name; 
    this.Mobile = Mobile; 
    this.Home = Home; 
    this.Work = Work; 
    this.PaymentType = PaymentType; 
    this.PaymentNote = PaymentNote; 
    this.DeliveryTime = DeliveryTime; 
    this.DeliveryInstructions = DeliveryInstructions; 
    this.DriverNotes = DriverNotes; 
    this.NoDeliveryReason = NoDeliveryReason; 
    this.IsDelivered = IsDelivered; 
} 
public static List<Run> createRuns(int itemCount) { 
    List<Run> runs = new ArrayList<>(); 
    SQLiteDatabase database; 
    try { 
     database=openDatabase("DriverPortalDB", null, OPEN_READONLY); 
     //Connection con = connectionClass.CONN(); 
     //if (con != null) { 
     if (database != null) { 
      int plusten = itemCount + 10; 
      Cursor c = database.rawQuery("SELECT * FROM RunsheetData where IsNewAndroid = '1' and id > '" + itemCount + "' and id <= '" + plusten + "'" , null); 
      int count = c.getCount(); 
      while(c.moveToNext()){ 

       Run run = new Run(Integer.parseInt(c.getString(0)), 
         Integer.parseInt(c.getString(1)), 
         Integer.parseInt(c.getString(2)), 
         Integer.parseInt(c.getString(3)), 
         c.getString(4), 
         Integer.parseInt(c.getString(5)), 
         c.getString(6), 
         c.getString(7), 
         c.getString(8), 
         c.getString(9), 
         c.getString(10), 
         c.getString(11), 
         c.getString(12), 
         c.getString(13), 
         c.getString(14), 
         c.getString(15), 
         c.getString(16), 
         c.getString(17), 
         c.getString(18)); 
       runs.add(run); 
      } 
     } 
    } 
    catch (Exception ex) { 
     //pbbar.setVisibility(View.GONE); 
     ex.printStackTrace(); 
    } 

    return runs; 
} 

我重視我的錯誤消息的屏幕截圖。

http://imgur.com/a/nmXbl

請讓我知道如果你需要更多的信息。

+1

我不認爲'Context.MODE.Private'是一個有效的標誌。它應該是'OPEN_READWRITE OPEN_READONLY CREATE_IF_NECESSARY和/或NO_LOCALIZED_COLLATORS'之一;至少根據https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase%28java.lang.String,%20android.database.sqlite.SQLiteDatabase.CursorFactory,%20int%29 – MikeT

+0

@MikeT我同意你的看法。我將標誌改爲OPEN_READONLY。但我得到了同樣的錯誤。 – venutamizh

+1

ooops錯過了第一個參數必須是路徑而不是數據庫名稱。 – MikeT

回答

1

的問題很可能與folloiwng代碼:

database=openDatabase("DriverPortalDB", null, Context.MODE.Private); 

第一paramater應該是路徑而不是僅僅是數據庫名稱,第二個問題是,Context.MODE。專用,第二參數不是指定標誌(它可能與指定標誌的值相關或可能不相關)中的一個。指定的標誌是: -

  • OPEN_READWRITE
  • OPEN_READONLY
  • CREATE_IF_NECESSARY
  • 和/或NO_LOCALIZED_COLLATORS

SQLiteDatabase public final class SQLiteDatabase

+0

P.S. ** Context,Mode.Private **似乎解析爲0 https://developer.android.com/reference/android/content/Context.html#MODE_PRIVATE。 ** OPEN_READWRITE **也出現解析爲0 https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#openDatabase%28java.lang.String,%20android.database.sqlite.SQLiteDatabase。 CursorFactory,%20int%29。所以** Context.MODE.Private **可能會工作。 – MikeT

相關問題