简体   繁体   中英

Portable Class Library for SQLite - no type validation?

I am using the new Portable Class Library (PCL) for SQLite for my universal app project in my PCL.

I can put strings to integer and I can put strings to fields which has a limited size. All values will be inserted without a warning or error.

And more I can create tables with no passed datatypes, or I can set anything as datatype. This let me think that all fields will be the same datatype (maybe a TEXT where you can anything put in). This aspect seems like it would impact performance. Do I have configure something else?

For example:

using (var c = new SQLiteConnection(_dbName))
        {
            using (var statement = c.Prepare(@"CREATE TABLE IF NOT EXISTS Test (
                                                                    Id INTEGER NOT NULL PRIMARY KEY,
                                                                    MyString LALALA NOT NULL,
                                                                    MyInt INTEGER NOT NULL);"))
            {
                statement.Step();
            }
        }

using (var statement = c.Prepare(@"INSERT INTO Test (MyString, MyInt)
                                        VALUES(@myString, @myInt);"))
            {
                statement.Bind("@myString", "hello tablehello tablehello tablehello tablehello tablehello tablehello table");
                statement.Bind("@myInt", "9asd");

                // Inserts data.
                statement.Step();

                // Resets the statement, to that it can be used again (with different parameters).
                statement.Reset();
                statement.ClearBindings();

                statement.Bind("@myString", "hello world");
                statement.Bind("@myInt", "asdasd");

                // Inserts data.
                statement.Step();
            }

And all does work - Why?

Also, it is possible to open the connection for readonly? In other sqlite libraries this is possible.

bye Markus

See http://www.sqlite.org/different.html under 'Manifest Typing'

Most SQL database engines use static typing. A datatype is associated with each column in a table and only values of that particular datatype are allowed to be stored in that column. SQLite relaxes this restriction by using manifest typing. In manifest typing, the datatype is a property of the value itself, not of the column in which the value is stored. SQLite thus allows the user to store any value of any datatype into any column regardless of the declared type of that column. (There are some exceptions to this rule: An INTEGER PRIMARY KEY column may only store integers. And SQLite attempts to coerce values into the declared datatype of the column when it can.)

As far as we can tell, the SQL language specification allows the use of manifest typing. Nevertheless, most other SQL database engines are statically typed and so some people feel that the use of manifest typing is a bug in SQLite. But the authors of SQLite feel very strongly that this is a feature. The use of manifest typing in SQLite is a deliberate design decision which has proven in practice to make SQLite more reliable and easier to use, especially when used in combination with dynamically typed programming languages such as Tcl and Python.

Now I don't necessarily believe that this is a good thing (I have never, ever believed that looser typing made anything 'more reliable'), but that is indeed how SQLite is meant to behave. Basically it ignores column types almost all of the time, so it's up to you to worry about whether you're putting the right values into them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM