2014-06-16 102 views
0

當我試圖用sqliteNET做任何事情時,我總是收到一個錯誤。我得到異常:使用SqliteNet時出現異常

near ")": syntax error 
    at SQLite.SQLite3.Prepare2 (IntPtr db, System.String query) [0x00029] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Database/SQLite.cs:3025 
    at SQLite.SQLiteCommand.Prepare() [0x00012] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Database/SQLite.cs:2190 
    at SQLite.SQLiteCommand.ExecuteNonQuery() [0x00026] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Database/SQLite.cs:2055 
    at SQLite.SQLiteConnection.Execute (System.String query, System.Object[] args) [0x00046] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Database/SQLite.cs:642 
    at SQLite.SQLiteConnection.CreateTable (System.Type ty, CreateFlags createFlags) [0x000a9] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Database/SQLite.cs:412 
    at SQLite.SQLiteConnection.CreateTable[URMMobileAccount] (CreateFlags createFlags) [0x00000] in <filename unknown>:0 
    at UltraRoute.URMLogin+<LoginToURM>c__AnonStorey1.<>m__0() [0x000bc] in /Users/ultradev/Projects/URM Mobile/UltraRoute/ViewControllers/URMLogin.cs:216 
    at MonoTouch.Foundation.NSAsyncActionDispatcher.Apply() [0x00000] in /Developer/MonoTouch/Source/maccore/src/Foundation/.pmcs-compat.NSAction.cs:90 
    at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr) 
    at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/.pmcs-compat.UIApplication.cs:38 
    at UltraRoute.Application.Main (System.String[] a) [0x0006a] in /Users/ultradev/Projects/URM Mobile/UltraRoute/Main.cs:66 

我的代碼如下所示:

using (var db = new SQLite.SQLiteConnection (Global.DatabasePath)) 
{   
    try 
    { 
     db.CreateTable<URMMobileAccount>(); 
     var localAccount = db.Query<URMMobileAccount> ("Select * from URMMobileAccount");              

     if (localAccount.Any()) 
     {     
      UsernameField.Text = localAccount [0].Username;     
     }  
    } 
    catch (Exception ex) 
    {    
     Global.Log (ex.Message, ex.StackTrace, LogLevel.SEVERE); 
    } 
} 

它未能在CREATE TABLE語句。

這裏是URMMobileAccount類:

public class URMMobileAccount 
{ 
    [PrimaryKey, AutoIncrement] 
    public int URMID {get;set;} 
    public int Id {get; set;} 
    public string Username {get; set;} 
    public string Password {get; set;} 
    public string Type {get; set;} 
    public Nullable<int> TypeId {get; set;} 
    public bool IsValid {get; set;} 
} 

我一直在尋找這個廣泛,它似乎無論出於何種原因,當它試圖創建,它通過反射得到所有的屬性表映射,然後再執行:

foreach(var p in props) 
{ 
    ... 

    if(p.CanWrite && !ignore) 
    { 
     cols.Add(new Column(p, createFlags)); 
    } 
} 

props是屬性的列表和colsList<Column>。忽略ignore變量p.CanWrite將爲該類中的所有屬性返回false?這不能寫,因爲CanWrite是由具有setter方法的屬性決定的,對吧?

+0

什麼是'URMMobileAccount'? –

+0

它是我定義的一個類...我將更新 – jharr100

+0

FWIW代碼在.NET下運行(我沒有得到錯誤),所以它可能與MonoTouch的反射實現有關。 –

回答

0

您使用的是最新的SQLite-net嗎?有很多人分發庫中,但只有一個正式版本,來源:

https://github.com/praeclarum/sqlite-net/tree/master/src

(很快就會有一個很好的PCL正式發佈,但我們仍然通過一些bug工作。 )

我剛纔寫的測試程序,一切工作正常:

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using MonoTouch.Foundation; 
using MonoTouch.UIKit; 
using SQLite; 

namespace SO24247435 
{ 
    public class URMMobileAccount 
    { 
     [PrimaryKey, AutoIncrement] 
     public int URMID {get;set;} 
     public int Id {get; set;} 
     public string Username {get; set;} 
     public string Password {get; set;} 
     public string Type {get; set;} 
     public Nullable<int> TypeId {get; set;} 
     public bool IsValid {get; set;} 
    } 

    [Register ("AppDelegate")] 
    public class AppDelegate : UIApplicationDelegate 
    { 
     UIWindow window; 

     public override bool FinishedLaunching (UIApplication app, NSDictionary options) 
     { 
      using (var db = new SQLiteConnection (
       Path.Combine(Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments),"test.sqlite"))) 
      {   
       try 
       { 
        db.CreateTable<URMMobileAccount>(); 
        var localAccount = db.Query<URMMobileAccount> ("Select * from URMMobileAccount");              

        if (localAccount.Any()) 
        { 
         Console.WriteLine (localAccount [0].Username); 
        }  
       } 
       catch (Exception ex) 
       {    
        Console.WriteLine (ex); 
       } 
      } 

      window = new UIWindow (UIScreen.MainScreen.Bounds); 
      // window.RootViewController = myViewController; 
      window.MakeKeyAndVisible(); 

      return true; 
     } 

     static void Main (string[] args) 
     { 
      UIApplication.Main (args, null, "AppDelegate"); 
     } 
    } 
} 
相關問題