-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
非同期処理プログラムの提出 #3
base: main
Are you sure you want to change the base?
Changes from 10 commits
f3d5f27
4e979ca
4d4cfe4
9fac0f2
09ae015
c67ad6a
03fda89
661ad6d
883d4c5
f01507a
54eacfb
956e3c3
7e59a66
31b4294
0ded943
c115759
9d82436
dc52ab1
0cc3111
f39a6b9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_02", "booktitle_03"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
const main = async () => { | ||
await run( | ||
db, | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
); | ||
|
||
for (const title of bookTitles) { | ||
const result = await run(db, "INSERT INTO books (title) VALUES (?)", [ | ||
title, | ||
]); | ||
console.log(`ADD TITLE: ${title}, ID: ${result.lastID}`); | ||
|
||
const book = await get(db, "SELECT * FROM books WHERE title = ?", [title]); | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} | ||
|
||
await run(db, "DROP TABLE books"); | ||
db.close(); | ||
}; | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import sqlite3 from "sqlite3"; | ||
import { run, get } from "./sqlite_utils.js"; | ||
|
||
const bookTitles = ["booktitle_01", "booktitle_01", "booktitle_02"]; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
const main = async () => { | ||
await run( | ||
db, | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
); | ||
|
||
for (const title of bookTitles) { | ||
try { | ||
const result = await run(db, "INSERT INTO books (title) VALUES (?)", [ | ||
title, | ||
]); | ||
console.log(`ADD TITLE: ${title}, ID: ${result.lastID}`); | ||
} catch (err) { | ||
if (err.code === "SQLITE_CONSTRAINT") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
try {
throw { code: "SQLITE_CONSTRAINT" }
} catch (...) {
...
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
if文に&条件を追加しました!
|
||
console.error(`ADD TITLE: ${title}, ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
|
||
try { | ||
const book = await get( | ||
db, | ||
"SELECT * FROM NotExistTable WHERE title = ?", | ||
[title], | ||
); | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} catch (err) { | ||
if (err.code === "SQLITE_ERROR") { | ||
console.error(`GET TITLE: ${title}, ${err.message}`); | ||
} else { | ||
throw err; | ||
} | ||
} | ||
} | ||
|
||
await run(db, "DROP TABLE books"); | ||
db.close(); | ||
}; | ||
|
||
main(); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitle = "booktitle_01"; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
() => { | ||
db.run("INSERT INTO books (title) VALUES (?)", [bookTitle], function () { | ||
console.log(`ADD TITLE: ${bookTitle}, ID: ${this.lastID}`); | ||
|
||
db.get("SELECT * FROM books WHERE title = ?", [bookTitle], (_, book) => { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
|
||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
}); | ||
}); | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import sqlite3 from "sqlite3"; | ||
|
||
const bookTitle = "booktitle_01"; | ||
const db = new sqlite3.Database(":memory:"); | ||
|
||
db.run( | ||
"CREATE TABLE books (id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL UNIQUE)", | ||
() => { | ||
db.run( | ||
"INSERT INTO NotExistTable (title) VALUES (?)", | ||
[bookTitle], | ||
function (err) { | ||
if (err) { | ||
console.error(`ADD TITLE: ${bookTitle}, ${err.message}`); | ||
} else { | ||
console.log(`ADD TITLE: ${bookTitle}, ID: ${this.lastID}`); | ||
} | ||
|
||
db.get( | ||
"SELECT * FROM NotExistTable WHERE title = ?", | ||
[bookTitle], | ||
(err, book) => { | ||
if (err) { | ||
console.error(`GET TITLE: ${bookTitle}, ${err.message}`); | ||
} else { | ||
console.log(`GET TITLE: ${book.title}, ID: ${book.id}`); | ||
} | ||
|
||
db.run("DROP TABLE books", () => { | ||
db.close(); | ||
}); | ||
}, | ||
); | ||
}, | ||
); | ||
}, | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
async 関数を定義するのではなく Top-level await を使ってください。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
修正しました!