์ฌ๋ฌ ๊ฐ์ PRIMARY KEY ์ง์ ํ๋ ๋ฐฉ๋ฒ
ํ ๊ฐ์ PRIMARY KEY๋ง ์ง์ ํ ๋์๋ column_name data_type PRIMARY KEY ์ ๊ฐ์ด ์ง์ ํ๋ฉด ๋์ง๋ง, ์ฌ๋ฌ ๊ฐ์ PRIMARY KEY๋ฅผ ์ง์ ํ ๋์๋ column๋ค์ ๋จผ์ ์จ์ค ๋ค์ PRIMARY KEY(col1, col2)๋ฅผ ์จ ์ค์ผ ํ๋ค.
sqlite> CREATE TABLE asdf (a int, b int, PRIMARY KEY(a,b) );
sqlite> INSERT INTO asdf VALUES(1,1);
sqlite> INSERT INTO asdf VALUES(1,1);
Runtime error: UNIQUE constraint failed: asdf.a, asdf.b (19)
sqlite> INSERT INTO asdf VALUES(1,2);
sqlite> INSERT INTO asdf VALUES(1,2);
Runtime error: UNIQUE constraint failed: asdf.a, asdf.b (19)
sqlite> INSERT INTO asdf VALUES(2,2);
sqlite> INSERT INTO asdf VALUES(2,1);
sqlite> SELECT * FROM asdf;
1|1
1|2
2|2
2|1
์ฌ๋ฌ ๊ฐ์ PK๊ฐ ์์ ๋์๋ ๋ชจ๋ PK์ ๊ฐ์ด ๋ชจ๋ ๊ฐ๊ฐ ์ผ์นํ๋ ๊ฒฝ์ฐ๋ง ์ถฉ๋๋ก ๊ฐ์ฃผํ๋ค.
๊ทธ๋ฆฌ๊ณ UNIQUE๋ ํ ๊ฐ์ด๋ ์ฌ๋ฌ ๊ฐ์ด๋ ์๊ด ์์ด ๊ทธ ํด๋น ์นผ๋ผ์์ ๊ฐ์ ๊ฐ์ด ์์ผ๋ฉด ๋ฌด์กฐ๊ฑด ์ถฉ๋์ ์ผ์ผํจ๋ค.
sqlite> CREATE TABLE asdf(a int UNIQUE,b int UNIQUE);
sqlite> INSERT INTO asdf VALUES(1,1);
sqlite> INSERT INTO asdf VALUES(1,2);
Runtime error: UNIQUE constraint failed: asdf.a (19)
sqlite> INSERT INTO asdf VALUES(2,1);
Runtime error: UNIQUE constraint failed: asdf.b (19)
sqlite> INSERT INTO asdf VALUES(2,2);
sqlite> SELECT * FROM asdf;
1|1
2|2
PK ์ถฉ๋ ์ INSERTํ์ง ์๋ ๋ฐฉ๋ฒ
INSERT OR IGNORE INTO๋ฅผ ์ฌ์ฉํ๋ฉด PK๊ฐ ์ถฉ๋ํ๋ ํ์ ๋ฌด์ํ๊ณ ๋ช ๋ น์ด๋ฅผ ์คํํ๋ค.
sqlite> CREATE TABLE IF NOT EXISTS User(Id INTEGER PRIMARY KEY,Name TEXT);
sqlite> INSERT INTO User VALUES(1,'Jeff'),(3,'John');
sqlite> INSERT INTO User VALUES(1,'Jiff'),(2,'Smith'),(3,'Julia'),(4,'Kotlin');
Runtime error: UNIQUE constraint failed: User.Id (19)
sqlite> INSERT OR IGNORE INTO User VALUES(1,'Jiff'),(2,'Smith'),(3,'Julia'),(4,'Kotlin');
sqlite> SELECT * FROM User;
1|Jeff
2|Smith
3|John
4|Kotlin
PK ์ค๋ณต ์ ๋ฎ์ด์ฐ๋ ๋ฐฉ๋ฒ
๋ค์๊ณผ ๊ฐ์ด ON CONFLICT DO UPDATE SET์ ์ฌ์ฉํ์ฌ PK ์ค๋ณต ์ ์์ ์ด ์ํ๋ column์ ๊ฐ์ ์ ๋ฐ์ดํธํ ์๋ ์๋ค.
sqlite> INSERT INTO User(Id,Name) VALUES(1,'Jiff'),(2,'Smiss'),(4,'Python'),(5,'C#') ON CONFLICT DO UPDATE SET Name=excluded.Name;
sqlite> SELECT * FROM User;
1|Jiff
2|Smiss
3|John
4|Python
5|C#
์ฌ๊ธฐ์๋ถํฐ๋ C#์์ SQLite๋ฅผ ์ฐ๋ ๊ฒ์ ๊ดํ ๋ด์ฉ์ด๋ค.
e_sqlite3 ์ค๋ฅ ํด๊ฒฐ ๋ฐฉ๋ฒ
System.DllNotFoundException: Unable to load DLL 'e_sqlite3' or one of its dependencies: ์ง์ ๋ ๋ชจ๋์ ์ฐพ์ ์ ์์ต๋๋ค. (0x8007007E)
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_config_none(SQLiteConfigOpsEnum op)
at System.Data.SQLite.UnsafeNativeMethods.sqlite3_config_none(SQLiteConfigOpsEnum op)
at System.Data.SQLite.SQLite3.StaticIsInitialized()
at System.Data.SQLite.SQLiteLog.PrivateInitialize(String className)
at System.Data.SQLite.SQLiteLog.Initialize(String className)
at System.Data.SQLite.SQLiteConnection..ctor(String connectionString, Boolean parseViaFramework)
at System.Data.SQLite.SQLiteConnection..ctor(String connectionString)
NuGet Package Manager์์ SourceGear.sqlite3์ ์ค์นํ๊ฑฐ๋ System.Data.SQLite.Core๋ฅผ ์ค์นํ๋ฉด ๋๋ค. System.Data.SQLite.Core์ ๋ ์ถ์ฒํ๋ค. SourceGear.sqlite3์ ์ค์นํ ๊ฒฝ์ฐ build target์ x64 ๋ฑ์ผ๋ก ๋ฐ๊ฟ์ค์ผ ํ ์๋ ์๋ค.
C#์์ connectionString์ data source ์ง์ ํ๋ ๋ฐฉ๋ฒ
Windows 10 ๊ธฐ์ค์ผ๋ก ์๋ ์ฝ๋์ฒ๋ผ ํ๋ฉด ๋๋ค. ํ์ผ์ด ์กด์ฌํ์ง ์์๋ ๋์ง๋ง, ํ์ผ์ ๋ด์ directory๋ ๋ฏธ๋ฆฌ ์์ฑ๋์ด ์์ด์ผ ํ๋ค. ๊ฒฝ๋ก์ ์ธ๋ฏธ์ฝ๋ก (";")์ด ํฌํจ๋์ด์์ ๊ฒฝ์ฐ ๋ฐ๋์ ํฐ ๋ฐ์ดํ๋ก ๊ฐ์ธ์ ์ ๋ฌํด์ค์ผ ํ๋ค.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SQLite;
namespace ConsoleApp100
{
internal static class Program
{
public static void Main(string[] args)
{
try
{
string l = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
l = Path.Combine(l, "not ex;st foใ
ฃd=r");
Directory.CreateDirectory(l);
l = Path.Combine(l, "tmp.db");
using (SQLiteConnection conn = new SQLiteConnection("Data Source=\"" + l + "\""))
{
conn.Open();
using (SQLiteCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "CREATE TABLE IF NOT EXISTS tmptable(tmpid TEXT PRIMARY KEY, tmpname TEXT)";
cmd.ExecuteNonQuery();
}
}
Console.WriteLine(l);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
}