sqlite3 problem when .blend file saves

Hi Guys,

I’m a little confused trying to use sqlite3 as database for my current project.
I’m trying to use a simple method inside of a class to handle the database. Something like this simplified code:

<code>
import sqlite3

class SQL():def init(self):
[INDENT=2]con = sqlite3.connect(‘c:\…\databank.db’) #just the location of the db file[/INDENT]
[INDENT=2]with con:[/INDENT]
[INDENT=3]cur = con.cursor()[/INDENT]
[INDENT=3]#Test if the table exists[/INDENT]
[INDENT=3]try:[/INDENT]
[INDENT=4]cur.execute(“SELECT * FROM gameObjects”)[/INDENT]
[INDENT=3]except:[/INDENT]
[INDENT=4]cur.execute(“CREATE TABLE gameObjects(Id INT, Name TEXT, position_X FLOAT, position_Y FLOAT, position_Z FLOAT)”)[/INDENT]
</code>
Then I instantiate the class by simply using:
sqldb = SQL()

Well, the thing is, when you create the blend file it works perfectly (if you open the .db file, the table is created successfully but when you SAVE the blend file it stops working.
When you run the script you get a error message saying:
“module” object has no attribute connect.
You simply cannot connect to your data bank. As if the sqlite3 module stops existing when you save your file.

Has anyone have this kind of problem?
Any solutions?

Anyways, thank you guys!

It does kind of sound like the name space has been cleared when a save is issued or your import is being undone. I ran into something similar using a save_as_mainfile in an event. I ended up using the @persistent decorator before the def. I don’t know a lot about it, but maybe you can use @persistent before a class to make it remain in memory. @persistent is a python feature you could search on.

I’ll take a look on this!
I’ll feedback the result!

Thanks!

No Atom, it did not do the trick!
This is really weird in my opinion! (noob opinion).

The code works perfectly untill you save the blend file, then, when you save it, you get this " ‘module’ object has no attribute connect" error.

BTW, the @persistent thingy expects a function, when you place it before a class you get an error. Seems like you need to place it right before the class method you need.

I’ll keep searching for the solution, maybe its something really dumb! LOL

Thanks!

Hi again guys,
I just felt like I needed to feedback on this issue as things just got a little more strange!
Well, I was saving am example blend file in an another folder to upload to here and when I tested it, it started working again! And the weird this is that, the when I saved the new file, the original one started working again. No changes made! see the screenshots of the error:
This was in the original testFile that was no working:


Then I used the same script on another blend file. Only changed the print statement that I used as control and:


The strange thing is that after I saved it to the new folder, both new and old files started working.
So I’m thinking Gnomes or gremlins? lol.

This puzzles me!

Anyways,

Thanks for the reply!