Advertisement
Guest User

Untitled

a guest
Mar 14th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <sqlite3.h>
  3.  
  4. //g++ -o test2 test2.cpp -lsqlite3
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int counter = 0;
  10.  
  11. sqlite3 *db;
  12. sqlite3_stmt * stmt;
  13.  
  14. string username = "panda";
  15. string name = "Kungfu Panda";
  16. string department = "normal";
  17. string password = "hellopassword";
  18.  
  19. string sqlstatement = "INSERT INTO abe_account (" + username + "," + name + "," + department + "," + password + ");";
  20.  
  21. if (sqlite3_open("abeserver.db", &db) == SQLITE_OK)
  22. {
  23. sqlite3_prepare( db, sqlstatement.c_str(), -1, &stmt, NULL );//preparing the statement
  24. sqlite3_step( stmt );//executing the statement
  25. }
  26. else
  27. {
  28. cout << "Failed to open dbn";
  29. }
  30.  
  31. sqlite3_finalize(stmt);
  32. sqlite3_close(db);
  33.  
  34.  
  35. return 0;
  36.  
  37. }
  38.  
  39. INSERT INTO tablename (field1, field2, field3) VALUES (value1, value2, value3);
  40.  
  41. INSERT INTO tablename VALUES (value1, value2, value3)
  42.  
  43. "('" + username + "','" + name + "','" + department + "','" + password + "');"
  44.  
  45. string quotesql( const string& s ) {
  46. return string("'") + s + string("'");
  47. }
  48.  
  49. "(" + quotesql(username) + "," + quotesql(name) + ...
  50.  
  51. string sqlstatement =
  52. "INSERT INTO abe_account (username, name, department, password) VALUES ("
  53. + quotesql(username) + ","
  54. + quotesql(name) + ","
  55. + quotesql(department) + ","
  56. + quotesql(password) + ");";
  57.  
  58. string sqlstatement = "INSERT INTO abe_account ('" + username + "','" + name + "','" + department + "','" + password + ");";
  59.  
  60. string sqlstatement = "INSERT INTO abe_account (ColumnName1, ColumnName2, ColumnName3, ColumnName4) VALUES ('" + username + "','" + name + "','" + department + "','" + password + "');";
  61.  
  62. int m = sqlite3_step(stmt);
  63. if(m == SQLITE_BUSY)
  64. {
  65.  
  66. if(m == SQLITE_ERROR)
  67. std::cout(sqlite3_sql(stmt), sqlite3_errmsg(db));
  68. if(m == SQLITE_MISUSE)
  69. std::cout(sqlite3_sql(stmt), sqlite3_errmsg(db));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement