SQLite: Difference between revisions

No edit summary
Line 24: Line 24:
import sqlite3
import sqlite3


# Connect to the database...
dbase = sqlite3.connect("foo.sqlite")
dbase = sqlite3.connect("foo.sqlite")
# A parameterized query with a cursor for processing the output
crs = dbase.cursor()
crs = dbase.cursor()
params = ("employees",)
params = ("employees",)
Line 32: Line 35:
     print(r)
     print(r)


# Close the database connection
dbase.close()
dbase.close()
</source>
</source>
Line 60: Line 64:
<tab name="C++">
<tab name="C++">
<source lang="cpp">
<source lang="cpp">
#include <iostream>
#include <string>
#include <string>
#include <sqlite3.h>
#include <sqlite3.h>
Line 65: Line 70:
int main(int argc,char** argv)
int main(int argc,char** argv)
{
{
  int ierr,age = 34;
   std::string query;
   std::string query;
   sqlite3* dbase;
   sqlite3* dbase;


   sqlite3_open("foo.sqlite",&dbase);
   ierr = sqlite3_open("foo.sqlite",&dbase);
   query = "SELECT * FROM " + std::string(argv[1]) + ";";
  if (ierr) {
   sqlite3_exec(dbase,query.c_str(),nullptr,nullptr,nullptr);
    std::cerr << "Unable to open database!" << std::endl;
    return 1;
  }
 
   query = "INSERT INTO employee(name,age) VALUES(John Smith," + std::to_string(age) + ");";
   ierr = sqlite3_exec(dbase,query.c_str(),nullptr,nullptr,nullptr);
  if (ierr != SQLITE_OK) {
    std::cerr << "Error in executing query!" << std::endl;
    return 2;
  }
 
   sqlite3_close(dbase);
   sqlite3_close(dbase);


Bureaucrats, cc_docs_admin, cc_staff
2,318

edits