June 202405:5618k views

SQLite is not weakly typed!

About

SQLite is "flexibly typed," which is somehow different than weakly typed. Or it can be strictly typed! Or it can be strictly and flexibly typed! The choice is yours.

Transcript

Everything I said

00:00

One of the things that you will hear about SQLite all the time is that SQLite is weakly typed, which is a little bit derogatory. For goodness sakes, it's flexibly typed. It's not weak, it's flexible. So the column types in SQLite are just merely suggestions. You can suggest what should go in there, but it will allow anything in there, unless you use a strict table.

00:25

Let's start with a non-strict table. If we create a table, let's do create table types, and we'll say that N is an integer, and we insert into types values one. What do you think's gonna happen? That's right. Select star from types. That sure is ugly. Let's do mode of box.

00:47

Whoops. Mode of box, and clear that out, and select again N of one. Perfect. Let's do select N and type of N from types. What do you think we get? Integer. Good so far. However, let's clear that out. Insert into types a value of hello, and that is wrong, because I forgot values, values hello.

01:17

So if we clear that out and select... Uh-oh. Now we have a text value in an integer column. You can put whatever you want in a SQLite column in a traditional table. We'll look at a strict table here in a second. Let's look at one more thing. If you were to insert into types values of 1.0, I want you to tell me what you think is gonna come back here.

01:41

Is it gonna store a float or a real or a double, or is it gonna store an integer? Are you ready? Do you have your answers ready? Here we go. Stored an integer. That's interesting. Let's try it again. Uh, 1.1. What do you think? Do you have your answers ready? Let's clear this out, and here we go.

02:04

Ah, that one stored a real. That sure is interesting. So the rule that we are intuiting here is that if SQLite can convert the input that you give it into the type that is suggested for that column without losing data, then it will. So in this case, when we inserted 1.0, it said, "I can convert that to an integer without losing data." However, 1.1, you can't just drop the .1.

02:34

That's valuable data, and so it just puts it in as a real. Is this a good or a bad thing? I don't know. It is a thing, though. If this is what you want, it's a good thing. If this is behavior you're not expecting, it's probably a bad thing. There are some places where this would be really valuable. You can imagine creating a key value store out of SQLite, and the value can be anything.

02:59

It can be a number, it can be a string, it could be a blob, it could be anything. That's, that's really helpful. You can imagine loading a CSV file into SQLite, and people that create CSV files don't always adhere to strict database rules, and they might, they might put some random stuff in there. That can be really useful. If, for whatever reason, you're using the entity attribute value paradigm, or let's take a better paradigm, user settings, and some settings are integers and some settings are

03:29

strings, this would be a great use case for that. What if you hate it? What if you freaking hate it, and you're like, "I want strict tables." We can do that. Let's take a look. If we were to create a table... So let's do, let's do another one. Let's do create table types two. I just came up with that name right off the spot.

03:49

Types two. N is an integer, and we append this keyword strict at the end. There you go. Now we've got a strict table. Insert into types two values of one. Ooh, we're all good. All right. Insert into types two value of hello. Type it out, come on. There we go.

04:10

Cannot store a text value in an integer column. SQLite has strict typing. But what if, what if you now have strict types and you're like, "You know what? I love strict types. I also want flexible types." Golly, you guys, you can have it all. You can have it all with SQLite.

04:32

Let's create a table called types three. Again, right off the spot. N, uh, integer, and we'll say, um, value, we can just call it value, is any. So now we can declare a strict table, but have a flexible column.

04:54

So we can insert into types three values is one and one, and insert into types three one and hello, and one and... Should change that from one to one to 3.1. Select star from types three.

05:15

That's ugly. Mode box. Select star from types three. So now N is strict and value is declared as an any, so you can put any thing that you want into that column. The next time you hear the haters say that SQLite is weakly typed, tell them that it's flexibly typed, and it could be strict type if you want.

05:38

If you wanna learn more about SQLite, I do have a course coming up called High Performance SQLite. If you wanna run SQLite in production, I encourage you to check out Turso. Until the next time, see ya.

Databases

More videos on databases

All videos

Get the next one by email

I’ll send you some more. Not too many.

Join 7,500+ other readers.

Read past issues