# SQLite In-Memory Table Demo using the SQLite Driver

**URL:** <https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364>\
**Category:** code\
**Tags:** sqlite\
**Created:** [June 11, 2020, 11:26pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364 "2020-06-11T23:26:18Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 11, 2020, 11:26pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/1 "2020-06-11T23:26:18Z")

</div>

Hello -

I discovered that you can easily use SQLite memory tables by way of the SQLite driver.  
Went ahead and made a simple app to demo it. I wrote it in C11, but it might work as far back as C9. Not sure.

This is an alpha version. If you have any bugs to report or suggestions, please holler.

I hope it can give you some ideas.

> **[GitHub - jslarve/SQLiteDriverDemo: Sample Application for using Clarion's SQLite...](https://github.com/jslarve/SQLiteDriverDemo)**
>
> Sample Application for using Clarion's SQLite Driver for in-Memory Tables

---

<div class="post-metadata">

**Author:** ![Rick\_UpperPark](https://clarionhub.com/user_avatar/clarionhub.com/rick_upperpark/32/346_2.png) [@Rick\_UpperPark](https://clarionhub.com/u/Rick_UpperPark)\
**Post date:** [June 11, 2020, 11:34pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/2 "2020-06-11T23:34:57Z")

</div>

Hey Jeff,  
Did you try any tests for performance against the regular in-memory driver?

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 12, 2020, 12:10am UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/3 "2020-06-12T00:10:28Z")

</div>

Hi Rick - No performance tests yet. I can see lots of uses for SQLite tables where you want to create stuff on the fly and be able to use SQL on it.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 12, 2020, 4:03am UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/4 "2020-06-12T04:03:03Z")

</div>

It’s really neat that you can do ATTACH DATABASE for loading additional SQlite disk files. So your OwnerID on the TurboSQL table might be just a single SQlite file, but you can load multiple SQLite files via ATTACH DATABASE.

If you have any SQLite files, you can test this right in my demo app.  
Try this:  
ATTACH DATABASE “.\YourSQLiteFile.SQLite” AS File1  
Then Execute that by pressing F8 (or Ctrl+Enter). There won’t be a response unless there’s an error.

Now you can list the tables in that database file:  
SELECT name FROM File1.sqlite\_master WHERE type = “table”

Hopefully, you see some table names there, which you can use to construct another query to list that table.

You can ATTACH multiple databases that way and query multiple tables at once.  
And it’s all managed within PROP:SQL.

I haven’t tried join yet, but that’s pretty powerful:

> <https://stackoverflow.com/questions/6824717/sqlite-how-do-you-join-tables-from-different-databases>

---

<div class="post-metadata">

**Author:** ![Graham\_Dawson](https://clarionhub.com/user_avatar/clarionhub.com/graham_dawson/32/1331_2.png) [@Graham\_Dawson](https://clarionhub.com/u/Graham_Dawson)\
**Post date:** [June 12, 2020, 1:45pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/5 "2020-06-12T13:45:59Z")

</div>

John Taylor did a lot of performance testing in his February 2007 article for ClarionMag

> **[ClarionMag monthly PDFs and source ZIPs (2007) - ClarionMag Archive - Confluence](https://clarionmag.jira.com/wiki/spaces/archive/pages/399450/ClarionMag+monthly+PDFs+and+source+ZIPs+2007)**

It’s the 2007-02 pdf and zip (source files) that contain his SQLite classes

---

<div class="post-metadata">

**Author:** ![elriba](https://clarionhub.com/letter_avatar_proxy/v4/letter/e/9f8e36/32.png) [@elriba](https://clarionhub.com/u/elriba)\
**Post date:** [June 12, 2020, 4:11pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/6 "2020-06-12T16:11:21Z")

</div>

Hi Jeff,  
Very nice example. I have a question, why do you call the windows API “InvalidateRect” on EVENT:SIZED?

This is the first time I have seen this done.  
Thanks!  
Edgard

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 12, 2020, 4:29pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/7 "2020-06-12T16:29:00Z")

</div>

Thanks Graham. I was trying to find that article but totally forgot the author’s name.

That’s where I got the “ATTACH” idea, but had not considered trying it in PROP:SQL before this week.

IMDD would certainly be more suitable for cases where you’d want to use appgen to create forms/browses. I don’t know if a TurboSQL table could be used in the same way (such as for inserting a record on a browse in the standard way). Seems like it would be more trouble than it’s worth in that regard.

I see tons of uses for the In-Memory SQLite, but not as a “which is better, IMDD or SQlite?” kind of thing.

But you can also use SQLite for non database related stuff too.

For example, in my demo app, try this:

!To create 4 random 10 character strings.  
SELECT hex(randomblob(10)) as R1,hex(randomblob(10)) as R2,hex(randomblob(10)) as R3,hex(randomblob(10)) as R4

!Or select an INT as hex (This requires a newer version of sqlite3.dll because the one that ships with Clarion doesn’t suport printf():):  
SELECT printf(‘%08X’, 99119191)

You can get the SQLIte version of your sqlite3.dll in the demo too. The version I just downloaded is 3.32.2, but the shipping one is 3.7.11. Support for printf() came in 3.8.x  
SELECT sqlite\_version() AS Ver

These functions are all for free and it’s really simple to implement them.

> **[Built-In Scalar SQL Functions](https://sqlite.org/lang_corefunc.html)**

And date/time stuff (with caveats) [Date And Time Functions](https://www.sqlite.org/lang_datefunc.html)

There’s a lotta neat stuff in there.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 12, 2020, 4:34pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/8 "2020-06-12T16:34:42Z")

</div>

Hi Edgard - Thanks. InvalidateRect forces a re-draw of the rect() in question. I was getting occasional artifacts on the screen when moving the controls around during resize. DISPLAY() would not clear them up so I used a bigger hammer..  
I used InvalidateRect() a lot on my C6 apps for certain situations. Maybe a different technique would have worked, but I was unsuccessful at figuring it out with plain Clarion code.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 12, 2020, 7:46pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/9 "2020-06-12T19:46:41Z")

</div>

I have updated the repository with some small improvements. instead of parsing sqlite\_master’s.sql to get the column names, pragma table\_info() seems to work fine.  
That allowed me to get rid some SystemString parsing that I didn’t like too much.

Also made it optional to display the data type.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 15, 2020, 3:35am UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/10 "2020-06-15T03:35:37Z")

</div>

Yet again, the repo has been updated.

Federico Navarro added a window for testing threads and demonstrates that the in-memory data is usable across threads until the turbosql table closes (which disposes of the in-memory data).

He also taught me that if you use ‘:memory:’ as the OWNER string (along with the ATTACH line) then you don’t need a disk file at all.

---

<div class="post-metadata">

**Author:** ![anon51095673](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@anon51095673](https://clarionhub.com/u/anon51095673)\
**Post date:** [June 15, 2020, 9:02am UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/11 "2020-06-15T09:02:25Z")

</div>

can some one explain this ? DummyDBOwner = ‘:memory:’ ! Per Federico Navarro, and it works!

what is this all about, looks interesting?

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 15, 2020, 2:46pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/12 "2020-06-15T14:46:39Z")

</div>

Stev - SQLite has a built in in-memory database called.“:memory:”. By setting the OWNER of the Clarion SQLite FILE to ‘:memory:’, a SQLite disk file is not needed.

However, in order to make use of the memory database, you still have to “ATTACH” to it. Then you can dynamically create and fill tables 100% in memory.

Some people have tried using ‘:memory:’ as an OWNER in the past, but since it “didn’t work”, it was assumed that in-memory SQLIte tables don’t work in the Clarion driver. But if they would have also used ‘ATTACH’ to attach to the memory database, it would have worked.

---

<div class="post-metadata">

**Author:** ![anon51095673](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@anon51095673](https://clarionhub.com/u/anon51095673)\
**Post date:** [June 15, 2020, 10:24pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/13 "2020-06-15T22:24:20Z")

</div>

thank you very much! where was this documented? and does it apply to any other drivers?

---

<div class="post-metadata">

**Author:** ![anon51095673](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@anon51095673](https://clarionhub.com/u/anon51095673)\
**Post date:** [June 15, 2020, 10:26pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/14 "2020-06-15T22:26:26Z")

</div>

becuase i would like to try it with the russian DLIB runtime databases driver source code we bought. i have found the russians in moscow but they cannot be contacted and we have taken over the source code since we paid a descent some for it.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 15, 2020, 10:44pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/15 "2020-06-15T22:44:42Z")

</div>

Stev - :memory: is not a Clarion specific thing it’s a built-in database inside SQLite. You can attach to and access it in Clarion via PROP:SQL as demonstrated in the demo. [In-Memory Databases](https://sqlite.org/inmemorydb.html)

---

<div class="post-metadata">

**Author:** ![anon51095673](https://clarionhub.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@anon51095673](https://clarionhub.com/u/anon51095673)\
**Post date:** [June 16, 2020, 11:20am UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/16 "2020-06-16T11:20:05Z")

</div>

i see thank you for the information.

---

<div class="post-metadata">

**Author:** ![jslarve](https://clarionhub.com/letter_avatar_proxy/v4/letter/j/e480ec/32.png) [@jslarve](https://clarionhub.com/u/jslarve)\
**Post date:** [June 22, 2020, 8:58pm UTC](https://clarionhub.com/t/sqlite-in-memory-table-demo-using-the-sqlite-driver/3364/17 "2020-06-22T20:58:25Z")

</div>

I modified the demo a little bit to demonstrate how fast it can be to load a table into memory.

Instead of pre-loading the data via an SQL script, the demo now uses a pre-built SQLite 198K record table. It adds 36MB to the repository size though.

On my my VM it takes less than .25 seconds to load those rows into memory. Desktop is about .13 seconds.

Queries are a bit slower than when there are only 500 rows, but it’s still pretty dang quick.  
I haven’t tested how indexes would impact the loading and query times, but queries are really fast without them.
