【问题标题】:Getting exception when using SqliteNet使用 SqliteNet 时出现异常
【发布时间】:2014-08-06 11:58:46
【问题描述】:

尝试使用 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);
     }
 }

创建表语句失败。

这里是 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&lt;Column&gt;。忽略 ignore 变量 p.CanWrite 为该类中的所有属性返回 false?不能这样写,因为 CanWrite 是由具有 setter 方法的属性确定的,对吧?

【问题讨论】:

  • 什么是URMMobileAccount
  • 它是我定义的一个类...我会更新
  • FWIW 您的代码在 .NET 下运行(我没有遇到错误),因此它可能与 MonoTouch 的反射实现有关。

标签: c# sqlite xamarin.ios syntax-error sqlite-net


【解决方案1】:

您使用的是最新的 SQLite-net 吗?有很多人在分发这个库,但只有一个正式版本,来源:

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

(很快就会有一个不错的 PCL 官方版本,但我们仍在解决一些错误。)

我刚刚编写了这个测试应用,一切正常:

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");
        }
    }
}

【讨论】:

    猜你喜欢
    • 2011-10-15
    • 2015-02-05
    • 1970-01-01
    • 2011-09-15
    • 2016-11-08
    • 2011-08-11
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多