Using Prepared statement with bind vars to pr...
  1. /*
  2. Requirements:
  3. Go tool chain
  4. go-sqlite3 requires C compiler
  5.  
  6. Install:
  7.  
  8. # Create empty dir. for project. & cd to it.
  9. go mod init mysql
  10. go get github.com
  11. export CGO_ENABLED=1
  12.  
  13. # paste code into file main.go
  14. # Upon save, if your compiler is founad, compilation of the driver will begin.
  15. # Monitor cpu usage to see when compiler/assembler completes.
  16.  
  17. # Create the sample DB in same directory as main.go file
  18. # using comment block at end of this code.
  19.  
  20. # Run without building:
  21. go run .
  22.  
  23. # Checking the table, it will not be dropped but, there will be bad data in the is_admin column.
  24. sqlite> select * from users where name = 'Fancy Pants';
  25. id name age country_code username password is_admin
  26. -- ----------- --- ------------ -------- ---------- -----------------------------
  27. 14 Fancy Pants 23 UK fp123 b00ze4L!f3 Robert'); DROP TABLE users;--
  28.  
  29. # NOTE: In it's current form it can only insert 1 record due to the UNIQUE
  30. # constaint on users.username field.
  31. */
  32.  
  33. package main
  34.  
  35. import (
  36. "fmt"
  37. "database/sql"
  38. "log"
  39. )
  40.  
  41.  
  42. func main() {
  43. dbfile := "./bd_sample.db"
  44. fmt.Println("Loading DB", dbfile)
  45.  
  46. db := loadDB(dbfile)
  47.  
  48. stmt, err := db.Prepare(`INSERT INTO users (name, age, country_code, username, password, is_admin)
  49. VALUES(?, ?, ?, ?, ?, ?)`)
  50. if err != nil {
  51. log.Fatal(err)
  52. }
  53. defer stmt.Close()
  54.  
  55. if _, err := stmt.Exec("Fancy Pants", 23, "UK",
  56. "fp123", "b00ze4L!f3", "Robert'); DROP TABLE users;--"); err != nil {
  57. log.Fatal(err)
  58. } else {
  59. log.Println("stmt Exec'd successfully.")
  60. }
  61.  
  62. }
  63.  
  64. func loadDB(dbDiskPath string) *sql.DB {
  65. dbDisk := openDB(dbDiskPath)
  66. return dbDisk
  67. }
  68.  
  69. // Get a handle to a SQLite DB.
  70. func openDB(dbPath string) *sql.DB {
  71. dbURL := "file:" + dbPath + "?_foreign_keys=on"
  72.  
  73. db, err := sql.Open("sqlite3", dbURL)
  74. if err != nil {
  75. panic(err)
  76. }
  77.  
  78. if err := db.Ping(); err != nil {
  79. log.Fatal(err)
  80. }
  81.  
  82. return db
  83. }
  84.  
  85. /*
  86. -- Sample DB Schema and data
  87. CREATE TABLE users (
  88. id INTEGER PRIMARY KEY,
  89. name TEXT NOT NULL,
  90. age INTEGER NOT NULL,
  91. country_code TEXT NOT NULL,
  92. username TEXT UNIQUE NOT NULL,
  93. password TEXT NOT NULL,
  94. is_admin BOOLEAN
  95. );
  96.  
  97. INSERT INTO
  98. users (id, name, age, country_code, username, password, is_admin)
  99. VALUES
  100. (1, 'David', 34, 'US', 'DavidDev', 'insertPractice', false);
  101. INSERT INTO
  102. users (id, name, age, country_code, username, password, is_admin)
  103. VALUES
  104. (2, 'Samantha', 29, 'BR', 'Sammy93', 'addingRecords!', false);
  105. INSERT INTO
  106. users (id, name, age, country_code, username, password, is_admin)
  107. VALUES
  108. (3, 'John', 39, 'CA', 'Jjdev21', 'welovebootdev', false);
  109. INSERT INTO
  110. users (id, name, age, country_code, username, password, is_admin)
  111. VALUES
  112. (4, 'Ram', 42, 'IN', 'Ram11c', 'thisSQLcourserocks', false);
  113. INSERT INTO
  114. users (id, name, age, country_code, username, password, is_admin)
  115. VALUES
  116. (5, 'Hunter', 30, 'US', 'Hdev92', 'backendDev', false);
  117. INSERT INTO
  118. users (id, name, age, country_code, username, password, is_admin)
  119. VALUES
  120. (6, 'Allan', 27, 'US', 'Alires', 'iLoveB00tdev', true);
  121. INSERT INTO
  122. users (id, name, age, country_code, username, password, is_admin)
  123. VALUES
  124. (7, 'Lance', 20, 'US', 'LanChr', 'b00tdevisbest', false);
  125. INSERT INTO
  126. users (id, name, age, country_code, username, password, is_admin)
  127. VALUES
  128. (8, 'Tiffany', 28, 'US', 'Tifferoon', 'autoincrement', true);
  129. INSERT INTO
  130. users (id, name, age, country_code, username, password, is_admin)
  131. VALUES
  132. (9, 'Lane', 27, 'US', 'wagslane', 'update_me', false);
  133. INSERT INTO
  134. users (id, name, age, country_code, username, password, is_admin)
  135. VALUES
  136. */
Pasted 2026-09-09 23:03:19

Short link: