SQLite for C#/SQL Developers

Current project uses sqlite3 for local storage of stuff for various reasons.  This was our first time working with it.  We’ve learned a few things that are not obvious.. coming from a SqlServer world —


SQLite Expert for the win.  It has a chocolatey package as well, although that failed to install once for me (but worked twice.  Who knows).

Note that SQLite is multi-process-open-able; data inserted by your program shows up automatically in the Data tab in SQLite Expert.  However, if SQLite Expert has the .db3 file open, can’t delete it to scrub it clean.

All the same: varchar nvarchar and text

SQLite doesn’t care.   It has 5 types of “data” it can store, depending on what the data is.  Handy reference: http://www.tutorialspoint.com/sqlite/sqlite_data_types.htm.

So when we were doing “uniqueidentifier” it was actually doing BLOB under the hood.

Its entirely dependent on what you’re trying to store, as to what gets stored.   So you can store a string in an int field.   https://www.sqlite.org/faq.html#q3  — its a feature, not a bug.

GUIDs as binary, beware Endian.

Most programmatic forms of storing Guid data end up storing it as Binary/Blob.   You know its binary if you do:

select id, hex(id)

And you get two things that look about the same size in length, but are shuffled around:



You might say “hey! bad Endian!”  but actually its Guid.ToByteArray() that’s doing the re-shuffling.

So, if you see the value {E589C1… in a grid result, and you want to select it, and you say

select .... where id=x'e589c1...'

That doesn’t work!  you have to give the bytes in the other order (as returned from hex(id)).

Foreign Keys Not On By Default from C#

This one was a shocker.  Its a pragma to turn foreign key checking on.  However, tools such as SQLite Expert do this automatically for you.

What we ended up with is a GetOpenDbConnection() call that did both .Open() and executed the pragma.

Logging Generated SQL from Dapper

A lot of this got figured out after we could see the SQL Generated by the various libraries we were working with. Turns out, that’s 2 statements after your connection is opened:

con.Flags |= SQLiteConnectionFlags.LogAll;
con.Trace += (o, e) => { Console.WriteLine("SQL: " + e.Statement); };

In Conclusion

These are the bombshells we’ve experienced over the last week.  Hopefully, all the bombshells are done with now.

Other than this learning curve — very solid, very fast, very nice.  2 Thumbs Up. Plus One. Heart It.

Tagged with:
Posted in Code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

.net 3d 3d-printing 4k abc15 algorithms ames android anonymous types asp.net audio editing aws backup basecamp beatunes biorhythm bittorrent blender blog boston marathon bpm c# caffeine campfire candycrush car carmax certification charity chiropractor cities-skylines clog clone codelouisville codepalousa coding coffee collaboration color run ComputerElbow ComputerVision configuration consulting cooking crash course crashplan crestwood cryptocurrency cycling dabda dan dapper DataSet ddl diabetes dictation dotnetcore dotnetmud downtown e-cycling elite excel exercise expiration facebook fashion feature-branching firefall flipflops Flow FL Studio focus food forecastle fortresscraft franklinplanner gadgets game-design games git github google docs google maps gopro gps grandpa greenshot hack half marathon headless health heart rate hiren ignew integration testing interop inventory ios ipad itunes javascript jobs karma kdf keyboards keys kittens lamont laptop lavalamp leaf lego life lifehack linq linqtotwitter linux los angeles louisville mandelbulber massage therapy mastery-teaching maths merge metformin Minecraft miniature modeling monitor mud muhammad ali institute music mvc mycartracks netfabb nexus10 node nomanssky nostalgia nutrition nwipe oldham county grand slam opal openjscad openscad owin pacedj paper mockup pepakura performance photoscan politics pomodoro postgresql powershell premiere priorities prius process product-management project-management qa ReFS resharper review rmi roman road 5k RSI rubiks running samsung 700t sandals schedule scooter scribblelive selenium service shapeways sleep slic3r sneakersync snot software software-engineering solidoodle soylent spacegame speaking sql sqlite SSDT SSIS standing state-machine stayfocusd stonehearth studying sunset tablet teaching team teamcity teamtreehouse terraform tesla testing tfs time timelapse torque touch tracks trs80 Tuple tutor twitter ubuntu unit testing utilities video video editing visual studio vscode vsvim warp stabilizer windows 8 windows home server wordpress wpf xml zozo
%d bloggers like this: