[LINUX] Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)

The contents that can be stored are the same regardless of the integer type (integer, int, long)

SQLite3 integer types (integer, int, long) are all Integer types. An example is shown below.

Shell command


cd /tmp
sqlite3 workdb.sqlite   #Enter this command from the terminal to launch the sqlite3 interpreter.
                        #workdb.sqlite is the file name. If it does not exist, a new one will be created.

#integer type,int type,Define a long type.
> create table t001 (i1 integer, i2 int, i3 long);
> insert into t001 values(1,2,3);
> insert into t001 values(123456789012345678,223456789012345678,323456789012345678);

#You can see that all items are of type integer.
> select typeof(i1), typeof(i2), typeof(i3) from t001;
integer|integer|integer
integer|integer|integer

#int type(4 bytes)You can see that it can be stored even if the value exceeds.
> select i1, i2, i3 from t001;
1|2|3
123456789012345678|223456789012345678|323456789012345678

So, when using the create table statement, I think it's better to use'integer'or the abbreviation'int' to specify an integer.

But if you're using a SQLite database from C #, that's a different story.

Below, I wrote a program to read SQLite3 data from C #. However, this program throws a System.OverflowException exception on the line adapter.Fill (dataset, "tmp") ;.

Program.cs


using System;
using System.Data;
using System.Xml;
using Mono.Data.Sqlite;

class MainClass
{
  public static void Main(string[] args)
  {
    SqliteConnection connection = new SqliteConnection("Data Source=/tmp/workdb.sqlite;Version=3;");
    connection.Open();

    string sql = "select * from t001;";
    using (SqliteDataAdapter adapter = new SqliteDataAdapter(sql, connection))
    using (DataSet dataset = new DataSet()) {
      adapter.Fill(dataset, "tmp");
      DataTable table = dataset.Tables["tmp"];
      foreach (DataRow row in table.Rows) {
        Console.WriteLine(row["i1"].ToString() + " " + row["i2"].ToString() + " " + row["i3"].ToString());
      }
    }
    connection.Close();
  }
}

Why the exception was thrown

This is because the value 223456789012345678 did not fit in the C # int (Value was either too large or too small for an Int 32.) because it was defined as'i2 int' in the create table statement.

SQLite3 type C#Type
integer long type(8 bytes)
int int type(4 bytes)
long long type(8 bytes)
(No model name) Data dependent(long type,string type,byte[]Mold etc.)

Precautions when using with C # (Mono.Data.Sqlite)

・ Int is not an abbreviation for integer. -C # (Mono.Data.Sqlite) recognizes integer as a long type, but C # programmers do not recognize it as integer = long type. -When using C # (Mono.Data.Sqlite), it is best to use int and long as the type name used in the create table statement.

at the end

The SQLite documentation does not specify that long can be used in the type name of the create table statement. Rather, SQLite3 doesn't recognize long types. You need'int'or'long' when Mono.Data.Sqlite determines the type.

Recommended Posts

Reasons to use long type in SQLite3 (C # Mono.Data.Sqlite)
How to use SQLite in Python
How to use Google Test in C
How to use the C library in Python
How to handle datetime type in python sqlite3
Reasons to use logarithm
Easy to use SQLite3
Use regular expressions in C
How to create and use static / dynamic libraries in C
Read json in C # and convert to dictionary type (forced)
Use <input type = "date"> in Flask
How to use classes in Theano
Mock in python-how to use mox
How to use Mysql in python
How to wrap C in Python
How to use ChemSpider in Python
How to use PubChem in Python
Use bash-completion to type long commands without looking at man or help
After all, what should I use to do type comparisons in Python?
How to use calculated columns in CASTable
Use the type features evolved in Sphinx-2.4
[Introduction to Python] How to use class in Python?
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
How to use Anaconda interpreter in PyCharm
How to use __slots__ in Python class
How to use regular expressions in Python
How to use Map in Android ViewPager
How to use is and == in Python
Type notes to Python scripts for running PyTorch model in C ++ with libtorch
[Python] How to use two types of type ()
How to generate permutations in Python and C ++
Type Python scripts to run in QGIS Processing
How to use Python Image Library in python3 series
How to multi-process exclusive control in C language
EP 11 Use `zip` to Process Iterators in Parallel
Use cryptography module to handle OpenSSL in Python
How to use tkinter with python in pyenv
Use ELMo, BERT, USE to detect anomalies in sentences
Use pygogo to get the log in json.
Use os.getenv to get environment variables in Python
C / Python> Read fwrite () value in C in Python> v0.1: 1 value / v0.2: 3 values / v0.3: corresponds to size_t / v0.4: Read double complex type