概要
cscの作法、調べてみた。
練習問題やってみた。
練習問題
sqlite3でboolを使え。
方針
- create table, insert into, select やってみる。
サンプルコード
using System;
using System.Text;
using System.IO;
using System.Data.SQLite;
namespace App
{
public class Program {
public static void Main(string[] args) {
bool check = true;
Console.WriteLine(check ? "Checked" : "Not checked");
SQLiteConnectionStringBuilder ConnectionStr = new SQLiteConnectionStringBuilder();
ConnectionStr.DataSource = "db0.sqlite";
using (SQLiteConnection Connection = new SQLiteConnection(ConnectionStr.ToString()))
{
Connection.Open();
using(SQLiteCommand Command = new SQLiteCommand(Connection))
{
Console.WriteLine("DROP TABLE");
Command.CommandText = "DROP TABLE user";
Command.ExecuteNonQuery();
Console.WriteLine("create table");
Command.CommandText = "create table user(id integer primary key, name text, updttm text, use bool)";
Command.ExecuteNonQuery();
Console.WriteLine("insert x 3");
Command.CommandText = "insert into user(id, name, updttm, use) values(1, 'hoge', '2022-10-26', True)";
Command.ExecuteNonQuery();
Command.CommandText = "insert into user(id, name, updttm, use) values(2, 'fuga', '2022-10-27', False)";
Command.ExecuteNonQuery();
Command.CommandText = "insert into user(id, name, updttm, use) values(3, 'piyo', '2022-10-28', True)";
Command.ExecuteNonQuery();
Console.WriteLine("SELECTE");
Command.CommandText = "SELECT * FROM user";
using (SQLiteDataReader Reader = Command.ExecuteReader())
{
while (Reader.Read())
{
int Id = Reader.GetInt32(0);
string Name = Reader.GetString(1);
string Updttm = Reader.GetString(2);
bool Use = Reader.GetBoolean(3);
Console.WriteLine(string.Format("id: {0} name: {1} updttm: {2} use: {3}", Id, Name, Updttm, Use));
}
}
}
}
}
}
}
実行結果
>sql0
Checked
DROP TABLE
create table
insert x 3
SELECTE
id: 1 name: hoge updttm: 2022-10-26 use: True
id: 2 name: fuga updttm: 2022-10-27 use: False
id: 3 name: piyo updttm: 2022-10-28 use: True
以上