Author Topic: SQLite for C# ~ Gareth Isaac  (Read 14566 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
« Last Edit: December 25, 2009, 07:57:09 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #1 on: December 25, 2009, 10:26:10 PM »
Great Articles!

Quote
"Any column in a version 3 database, except an INTEGER PRIMARY KEY column, may be used to store any type of value."


This is one of the features I like, especially for dynamic languages like lisp. Caveat, it's like a box of chocolates, you don't what you're going to get.

example

"CREATE TABLE MyTable (No int, FirstName char(64), LastName char(64), age int);"

"INSERT INTO MyTable VALUES (1, 'Donald', 'Luck ' , 99);"
"INSERT INTO MyTable VALUES (2, 'Mickey', 'Mouse' , 101);"
"INSERT INTO MyTable VALUES (3, 'Minni' , 'Mouse' , 'Never ask a lady how old she is;);"


Select * returns (  ("No" "FirstName" "LastName" "age")
                          (1 "Donald" "Luck " 99)
                          (2 "Mickey" "Mouse" 101)
                          (3 "Minni" "Mouse" "Never ask a lady how old she is"))

so...

select AVG(age) from MyTable;"  would return an incorrect value as the 'Minni' record would    return 0. 

(("AVG(age)") (66.6667))

Just thinking out loud, it wouldn't be hard to wrap the stuff I have written for lisp for .net, it would eliminate the need for providers....

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #2 on: December 25, 2009, 10:34:12 PM »
< .. >
Just thinking out loud, it wouldn't be hard to wrap the stuff I have written for lisp for .net, it would eliminate the need for providers....


name your price !!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #3 on: December 25, 2009, 10:41:05 PM »
 :lol:

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #4 on: December 26, 2009, 06:35:23 AM »
I had started wrapping the C++ code I use in SQLiteLsp http://www.codeproject.com/KB/database/CppSQLite.aspx ,

when I found it had already been done
http://code.google.com/p/managed-sqlite/

I'm going to modify a few things, link it with the latest SQLite, use .NET 3.5  etc..

hopefully samples will be coming soon

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #5 on: December 26, 2009, 11:07:05 AM »
Got it to work,  I have attached a sample solution, note the SQLiteNET.dll (32bit) is in the debug folder. it's not as heavily wrapped as the version for lisp, I also want to make a few changes.  Thoughts? suggestions?

Code: [Select]
namespace CsMgdAcad2
{
 public class Commands
 {

  // these should be an enum
  const int SQLITE_INTEGER = 1;
  const int SQLITE_FLOAT = 2;
  const int SQLITE_BLOB = 4;
  const int SQLITE_NULL = 5;
  const int SQLITE_TEXT = 3;

  [CommandMethod("doit")]
  static public void doit()
  {
   try
   {
    Editor editor = AcAp.Application.DocumentManager.MdiActiveDocument.Editor;
    List<List<object>> table = new List<List<object>>();

    // dispose or die
    using (ManagedSQLite.Database db = new ManagedSQLite.Database())
    {
     db.Open("C:\\MySQLite.db");

     // dispose or die
     using (ManagedSQLite.Query q = db.ExecuteQuery("SELECT * FROM MyTable;"))
     {
      while (!q.EndOfFile()) // I think this should be a property not a function
      {
       List<object> row = new List<object>();

       // should NumberOfFields() this be a property?
       for (int i = 0; i < q.NumberOfFields(); i++)
       {
        switch (q.FieldDataType(i))
        {
         case SQLITE_INTEGER: // these should be an enum
          row.Add(q.GetIntField(i));
          break;
         case SQLITE_FLOAT: // these should be an enum
          row.Add(q.GetFloatField(i));
          break;
         case SQLITE_NULL: // these should be an enum
          break;
         default:
          row.Add(q.GetStringField(i));
          break;
        }
       }
       q.NextRow();
       table.Add(row);
      }
     }
    }

    foreach(List<object> list in table)
    {
     editor.WriteMessage("\n(");
     foreach (object item in list)
     {
      editor.WriteMessage("{0} ",item);
     }
     editor.WriteMessage(")");
    }
   }
   catch (System.Exception e)
   {
    AcAp.Application.DocumentManager.MdiActiveDocument.
     Editor.WriteMessage("\n{0}", e.Message);
   }
  }
 }
}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #6 on: December 26, 2009, 06:52:48 PM »

Thanks Dan, I'll have a look tonight.


I had this bookmarked for a look as well
http://www.devart.com/dotconnect/sqlite/docs/

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #7 on: December 26, 2009, 07:10:04 PM »

Dan, would we need seperate  x86, IA64 and x64 builds ??
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #8 on: December 26, 2009, 08:41:33 PM »



There are not enough hours in the day !!
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #9 on: December 26, 2009, 08:56:34 PM »

Dan, would we need seperate  x86, IA64 and x64 builds ??


Right, we would need two builds, one for win32 and one for x64 to keep the native SQLite code happy.
I am not adding anything that would make it version dependant, so it should run with any .NET 2.0+ assembly/exe.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #10 on: December 26, 2009, 09:07:20 PM »
made a few changes  :-)

Code: [Select]
   // added a new ctor to consume the path and open the db
    using (ManagedSQLite.Database db = new ManagedSQLite.Database("C:\\MySQLite.db"))
    {
     // dispose or die
     using (ManagedSQLite.Query q = db.ExecuteQuery("SELECT * FROM MyTable;"))
     {
      while (!q.IsEndOfFile) // is now a property
      {
       List<object> row = new List<object>();
       for (int i = 0; i < q.NumberOfFields; i++) // NumberOfFields is now property
       {
        switch (q.FieldDataType(i))
        {
         case ManagedSQLite.SQLiteType.Integer: // added SQLiteType enum
          row.Add(q.GetIntField(i));
          break;
         case ManagedSQLite.SQLiteType.Double:
          row.Add(q.GetFloatField(i));
          break;
         case ManagedSQLite.SQLiteType.Null:
          break;
         default:
          row.Add(q.GetStringField(i));
          break;
        }
       }
       q.NextRow();
       table.Add(row);
      }
     }
    }

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #11 on: December 26, 2009, 09:09:49 PM »


Yes Daniel, better I think


      while (!q.IsEndOfFile) // is now a property     
     {
       List<object> row = new List<object>();
       for (int i = 0; i < q.NumberOfFields; i++) // NumberOfFields is now property
       {
        switch (q.FieldDataType(i))
        {
         case ManagedSQLite.SQLiteType.Integer: // added SQLiteType enum
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #12 on: December 26, 2009, 09:56:20 PM »
Oh I see you snuck in IA64 : ) I don't know about that one  :-o

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: SQLite for C# ~ Gareth Isaac
« Reply #13 on: December 26, 2009, 10:24:29 PM »
Oh I see you snuck in IA64 : ) I don't know about that one  :-o

I just copied the textstring from the SQLite site .... don't think IA64 would be necessary.  :)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

It's Alive!

  • Retired
  • Needs a day job
  • Posts: 8690
  • AKA Daniel
Re: SQLite for C# ~ Gareth Isaac
« Reply #14 on: December 26, 2009, 10:47:23 PM »
Here is the solution with win32 and x64 builds..
« Last Edit: December 27, 2009, 01:48:34 AM by Daniel »