问题
I keep getting an error when attempting to do anything with sqliteNET. I get exception:
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
My code looks like:
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);
}
}
It fails at the create table statement.
Here is URMMobileAccount class:
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;}
}
I've been looking into this extensively and it seems for whatever reason that when it tries to create the table mappings that it gets all the properties via reflection and then does:
foreach(var p in props)
{
...
if(p.CanWrite && !ignore)
{
cols.Add(new Column(p, createFlags));
}
}
props
is the list of properties and cols
is a List<Column>
. Ignoring the ignore
variable p.CanWrite returns false for all the properties in that class? That couldn't be write as CanWrite is determined by the property having a setter method, right?
回答1:
Are you using the most recent SQLite-net? There are lots of people distributing the library, but there is only one official version, the source:
https://github.com/praeclarum/sqlite-net/tree/master/src
(Soon there will be a nice PCL official release, but we're still working through some bugs.)
I just wrote this test app and everything works fine:
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");
}
}
}
来源:https://stackoverflow.com/questions/24247435/getting-exception-when-using-sqlitenet