SQLite in RhinoPython

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?

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.

2 Likes

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.

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

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!

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

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

1 Like

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:

Ok, cool :smile: glad I could help…

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?

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?

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:

2 Likes

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!

Thank you, finally managed to make it worked using SQL db by this db management system. Didn’t know much about compatibility so had to google it.

For information purposes: The SQLite module is included with Rhino 6 now, so it’s not necessary anymore to install it through this way :slight_smile:

1 Like

Hi ! Python Newb here, and learning MySQL.
Is there any help / reference / tutorials for SQLite in RhinoPython ?

Have you tried referring to the standard CPython documentation? I don’t know if they followed that approach or not, but it might be worth trying it.

I don’t have Rhino 6 with me here, so I can’t try it. What do you want to do with it?

Hi Andrew, thanks for the link.
I want to store info about my blocks (attributes, Rhino ID, volume, area, length, etc…) and make queries through a human UI interface to display the filtered info and evntually select the corresponding objects in Rhino.
It is also the basis for generation of BOM tables, fabrication drawings, etc.

Since it’s embedded in Rhino 6, I would expect a minimum of documentation from McNeel’s part ; at least a basic example of how to use in Rhino or GH.

Now that I understand what database queries can do, I feel that a lot of GH tree management mumbo-jumbo could be eliminated when needing to sort/filter data…
I’m kind of surprised to find no actual examples of definitions using Slingshot or GHPython + SQLight in this forum.

OK, this is for the folks like me who are trying to get on the saddle :

Currently following this excellent video tutorial about SQLite3 and Python.

Lil’ question here : is it at all possible to store Rhino geometry as goo in a SQLite database ?
My first attempt was a failure

If you were to use Rhino 7 you could serialize your geometry to JSON (that is a string) with ToJSON, and put that in your database.

Then you’d deserialize with FromJSON.