简体   繁体   中英

Can we use all SQL queries and functions on SQLite database in Android?

I have a database on SQL Server which contain almost 40 tables. These tables have Primary, Foreign, and Composite keys in them. Other than that on execution time I have used multiple complex Queries which uses Joins and Functions and other SQL parameters. I want to know that all the queries which are applicable on SQL Server are also applicable on SQLite running on Android? Thanks

I want to know that all the queries which are applicable on SQL server are also applicable on SQLite running on android?

I'd suggest that the best way to ascertain this is to test using one of the available SQLite tools (eg DBBeaver, Navicat for SQLite).

JOINS As an example of incompatibility there is no RIGHT JOIN all joins are LEFT as such in SQLite so you would need to reverse a RIGHT JOIN.

as per https://sqlite.org/syntax/join-operator.html

Column Types In some other aspects SQLite is more than hospitable, such as column types, with one exception a row/column can store any of the supported types (NULL, INTEGER, TEXT, REAL, BLOB, NUMERIC) and you can actually specific virtually any column type eg any_old_column_type is valid as a column type.

  • NUMERIC is a catchall in that a column type that is not determined as any of the other types via some basic rules is numeric any_old_column_type would have a type affinity of NUMERIC as an example. Not that type affinity is very frequently a matter of concern.

  • example here at http://sqlfiddle.com/#!7/9eecb7/8070

I believe SQL Server has a DATE type SQLite doesn't but can handle dates stored as integers, strings, real or numeric

You may have to be wary of nulls as you should never use = null (as no null is the same as another null) rather you would use IS null or IS NOT null. (not sure about SQL Server).

Auto Incrementing Yet another difference that you may well encounter is with auto incrementing identifiers.

In short in SQLite a column definition that is explicitly or implicitly (BUT EXACTLY INTEGER and not INT or PINT (which would have INTEGER affinity) ....):- column_name INTEGER PRIMARY KEY , will increment typically monotonically (and with a tweak (insert - number) can roll through to negatives (INTEGER is 64bit signed)) BUT there is no guarantee about it being monotonically.

Often the keyword AUTOINCREMENT is assumed to define this action. However, what AUTOINCREMENT does is guarantee that the value will be greater than any used (or fail with SQLITE FULL error should the ID exceed 9223372036854775807, without AUTOINCREMENT a lower "free" id may be returned (very likely as at 9223372036854775807 id's it is unlikely that the data could be stored)).

  • the negative tweak cannot be used for AUTOINCREMENT

  • ROWID There is (unless the WITHOUT ROWID) always such a column, but it is normally hidden, it can be referred to as rowid, rowid or oid. column_name INTEGER PRIMARY KEY (with or without AUTOINCREMENT) makes the column an alias of the rowid.

  • see https://sqlite.org/autoinc.html

  • perhaps note that AUTOINCREMENT is not recommended unless necessary as it has overheads (a table sqlite_sequence where the highest provided id is stored according to the table's name).

Android Version Another consideration is that different versions of Android have different version of SQLite and therefore the lowest level of Android targeted may determine what SQL can or can't be utilised.

One such concern could be Windows function that use the OVER keyword. Only Android API 30+ has such functionality.

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