-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mysql - kotlin - commands.txt
65 lines (50 loc) · 1.41 KB
/
Mysql - kotlin - commands.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Insert data
database.insert(NotesEntity){ notes ->
set(notes.note, "Learning ktor")
}
// Fetch data from db 1
val notes = database.from(NotesEntity).select()
notes.forEach { row ->
println("${row[NotesEntity.id]} : ${row[NotesEntity.note]}")
}
// Fetch data from db 2
val notes = db.from(NotesEntity).select()
.map {
val id = it[NotesEntity.id]
val note = it[NotesEntity.note]
NotesModel(id ?: -1,note ?: "")
}
// Update data
database.update(NotesEntity) {notes ->
// Change to data id = 4 to -> "Working mysql"
set(notes.note, "Working mysql")
where {
notes.id eq 4
}
}
// Delete data
database.delete(NotesEntity) { notes ->
// Delete data at id = 3
notes.id eq 3
}
// Get data by id
val idReq = call.parameters["id"]?.toInt() ?: -1
val note = db.from(NotesEntity)
.select()
.where { NotesEntity.id eq idReq }
.map {
val id = it[NotesEntity.id]!!
val note = it[NotesEntity.note]!!
NotesModel(id = id, note = note)
}.firstOrNull()
MYSQL :
CREATE DATABASE notes;
USE notes;
CREATE TABLE notes(
id int NOT NULL AUTO_INCREMENT,
note varchar(1500) NOT NULL,
PRIMARY KEY(id)
)
CALL :
use notes;
Select * from note;