SQLite in RhinoPython

rhino
python

(Max Eschenbach) #1

Hey Folks,
I am looking for a way to use SQLite in RhinoPython.
Since Rhino uses IronPython 2.7 and the sqlite3 module is part of the standard library since Python 2.5, I don’t quite understand why I can’t just call:

import sqlite3

This will give me an error, since the module is not available for some reason.
Can anyone explain / help?


Sqlite3 in Grasshopper
Rhino Python Editor - Connect to database
(Alain Cormier) #2

Hi,

It’s because sqlite3 wraps sqlite which is writen in C and you can’t load native libraries in IronPython.

A google search gave me this link but IronPython.SQLite seems like an old project.


(Max Eschenbach) #3

Thanks a lot for the reply and the information given!
At least now i know why it is’nt working :smile:

Just tried to do it with IronPython.SQLite but it seems that the RhinoIronPython does not check the DLLs folder :confused:

So maybe I have to use some other database system.


(Menno Deij - van Rijswijk) #4

Maybe you can use this .net interface for sqlite.
https://system.data.sqlite.org


(Max Eschenbach) #5

Yeah I also stumbled upon that yesterday.
But given my lack of knowledge regarding .NET things, I can not get it to run at all.
I tried to install it for .NET 4.0 (as I think Rhino uses 4.0 but I’m not even sure of that :confused:) and then tried calling

import System.Data.SQLite

but other than an error I couldn’t get anything.
If someone has any knowledge about how to get SQLite running within python scripts it would be greatly apperciated!


(Menno Deij - van Rijswijk) #6

It seems that you are not the only one wanting to use sqlite in IronPython.

https://bitbucket.org/jdhardy/ironpython.sqlite/wiki/Home


(Max Eschenbach) #7

Thank a LOT!
This was actually a great hint and led to me figuring out how to do it.
In case somebody else wants to use, I’ll list the steps:

  • Get IronPython.SQLite (this seems discontinued but it still worked)

  • Copy IronPython.SQLite.dll and sqlite3 into a Folder that has been added to your library search paths within RhinoPython (alternatively you could copy it anywhere you want and add the location to the library search paths)

  • Edit the __ini__.py File in the sqlite3 Folder and add the following two lines on the top:

    import clr
    clr.AddReferenceToFile(“IronPython.SQLite.dll”)

  • The last step ensured that the DLL is found

  • Now if we want to use the sqlite3 module in our script we can simply say:

    import sqlite3

  • And now just use it as described in 11.13. sqlite3 — DB-API 2.0 interface for SQLite database

Have fun with sqlite3! :smile:


(Menno Deij - van Rijswijk) #8

Ok, cool :smile: glad I could help…


#9

Sorry for resurrecting an old thread, but it addresses what I am trying to do and I can’t get it to work at present. I think I have followed the instructions, but it is still not working.

I have run the EditPythonScript command in Rhino and have identified one of the folders (by going into Tools > Options:
C:\Users\myname\AppData\Roaming\McNeel\Rhinoceros\5.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib

This means that in this folder I now have the DLL and the sqlite3 folder, inside which is the __init__.py file which I have edited…

import clr
clr.AddReferenceToFile(“IronPython.SQLite.dll”)
from dbapi2 import *

I then create a component and put in

import sqlite3

However, this does not work… I get an interesting error:

Runtime error (SyntaxErrorException): unexpected token '“'
Traceback:
  line 1, in script

I wonder if I should be adding the sqlite3 folder to a python path?


#10

OK… so I have managed to get it to work…

I have created a custom folder C:\Utilities\IPythonSQLite3 and have placed the DLL and sqlite3 folder in there. I have then added the folder to the PythonScript path in Tools > Options (as above)

I have not edited the __init__.py file.

Instead I have placed the following lines in my component.

import sys
sys.path.append(r"C:\Utilities\IPythonSQLite3")
import clr
clr.AddReference("IronPython.SQLite.dll")
import sqlite3

After this, I can access the sqlite3 library. However, it seems wrong to do this with every component. Can anyone point to another way of doing this? Is there a startup script that I could place this in?


(Max Eschenbach) #11

Hey, sorry for getting back to you just now.
I don’t quite understand, why editing the __init__.py is not working for you. I have tried it again just now with Rhino 5 & 6 and it was working fine for me. So unfortunately I can’t really reproduce your error.

Anyway, the __init__.py inside the sqlite3 folder should work fine as you startup script, if you edit it and add…

import sys
sys.path.append(r"C:\Utilities\IPythonSQLite3")
import clr
clr.AddReference("IronPython.SQLite.dll")

…to the top of the file, you should be able to just call import sqlite3 inside of your scripts.

Hope I could help :slight_smile:


#12

Wonderful - it does what it is supposed to now. I was a little tired and didn’t think of that.

Thanks for the quick response (less than two hours) on a thread that was over two years old!