Browse Source

initial playing with sqlite3 in c

Steve Thielemann 4 years ago
commit
140c55454a
5 changed files with 139 additions and 0 deletions
  1. 8 0
      CMakeLists.txt
  2. 38 0
      FindSQLite3.cmake
  3. 38 0
      cmake/FindSQLite3.cmake
  4. 55 0
      dataplay.c
  5. BIN
      test.db

+ 8 - 0
CMakeLists.txt

@@ -0,0 +1,8 @@
+cmake_minimum_required(VERSION 3.0)
+project(testdoors)
+set (CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
+find_package(SQLite3 REQUIRED)
+# add_subdirectory(MagiDoor)
+include_directories(${SQLITE3_INCLUDE_DIRS})
+add_executable(dataplay dataplay.c)
+target_link_libraries(dataplay ${SQLITE3_LIBRARIES})

+ 38 - 0
FindSQLite3.cmake

@@ -0,0 +1,38 @@
+# Copyright (C) 2007-2009 LuaDist.
+# Created by Peter Kapec <[email protected]>
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
+# For details see the COPYRIGHT file distributed with LuaDist.
+#	Note:
+#		Searching headers and libraries is very simple and is NOT as powerful as scripts
+#		distributed with CMake, because LuaDist defines directories to search for.
+#		Everyone is encouraged to contact the author with improvements. Maybe this file
+#		becomes part of CMake distribution sometimes.
+
+# - Find sqlite3
+# Find the native SQLITE3 headers and libraries.
+#
+# SQLITE3_INCLUDE_DIRS	- where to find sqlite3.h, etc.
+# SQLITE3_LIBRARIES	- List of libraries when using sqlite.
+# SQLITE3_FOUND	- True if sqlite found.
+
+# Look for the header file.
+FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h)
+
+# Look for the library.
+#FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite)
+FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3)
+
+# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
+
+# Copy the results to the output variables.
+IF(SQLITE3_FOUND)
+	SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
+	SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
+ELSE(SQLITE3_FOUND)
+	SET(SQLITE3_LIBRARIES)
+	SET(SQLITE3_INCLUDE_DIRS)
+ENDIF(SQLITE3_FOUND)
+
+MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)

+ 38 - 0
cmake/FindSQLite3.cmake

@@ -0,0 +1,38 @@
+# Copyright (C) 2007-2009 LuaDist.
+# Created by Peter Kapec <[email protected]>
+# Redistribution and use of this file is allowed according to the terms of the MIT license.
+# For details see the COPYRIGHT file distributed with LuaDist.
+#	Note:
+#		Searching headers and libraries is very simple and is NOT as powerful as scripts
+#		distributed with CMake, because LuaDist defines directories to search for.
+#		Everyone is encouraged to contact the author with improvements. Maybe this file
+#		becomes part of CMake distribution sometimes.
+
+# - Find sqlite3
+# Find the native SQLITE3 headers and libraries.
+#
+# SQLITE3_INCLUDE_DIRS	- where to find sqlite3.h, etc.
+# SQLITE3_LIBRARIES	- List of libraries when using sqlite.
+# SQLITE3_FOUND	- True if sqlite found.
+
+# Look for the header file.
+FIND_PATH(SQLITE3_INCLUDE_DIR NAMES sqlite3.h)
+
+# Look for the library.
+#FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite)
+FIND_LIBRARY(SQLITE3_LIBRARY NAMES sqlite3)
+
+# Handle the QUIETLY and REQUIRED arguments and set SQLITE3_FOUND to TRUE if all listed variables are TRUE.
+INCLUDE(FindPackageHandleStandardArgs)
+FIND_PACKAGE_HANDLE_STANDARD_ARGS(SQLITE3 DEFAULT_MSG SQLITE3_LIBRARY SQLITE3_INCLUDE_DIR)
+
+# Copy the results to the output variables.
+IF(SQLITE3_FOUND)
+	SET(SQLITE3_LIBRARIES ${SQLITE3_LIBRARY})
+	SET(SQLITE3_INCLUDE_DIRS ${SQLITE3_INCLUDE_DIR})
+ELSE(SQLITE3_FOUND)
+	SET(SQLITE3_LIBRARIES)
+	SET(SQLITE3_INCLUDE_DIRS)
+ENDIF(SQLITE3_FOUND)
+
+MARK_AS_ADVANCED(SQLITE3_INCLUDE_DIRS SQLITE3_LIBRARIES)

+ 55 - 0
dataplay.c

@@ -0,0 +1,55 @@
+#include <sqlite3.h>
+#include <stdio.h>
+#include <string.h>
+
+void datado(void) {
+  sqlite3 *db;
+  char sqlbuffer[256];
+  sqlite3_stmt *stmt;
+  int rc;
+  const char *database = "test.db";
+
+  rc = sqlite3_open(database, &db);
+  if (rc) {
+    printf("Error opening database %s : %s", database, sqlite3_errmsg(db));
+    return;
+  }
+  // unsure
+  sqlite3_busy_timeout(db, 5000);
+  strcpy(sqlbuffer, "SELECT * FROM USER;");
+  sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
+  while (sqlite3_step(stmt) == SQLITE_ROW) {
+    // total columns = sqlite_3_column_count(stmt);
+    int i = sqlite3_column_int(stmt, 0);
+    printf("%s %d : %s %s / %s %s\n", sqlite3_column_name(stmt, 0), i,
+           sqlite3_column_name(stmt, 1), sqlite3_column_text(stmt, 1),
+           sqlite3_column_name(stmt, 2), sqlite3_column_text(stmt, 2));
+  };
+  sqlite3_finalize(stmt);
+  // for now
+
+  // NEXT!
+  char nick[] = "Captain";
+
+  strcpy(sqlbuffer, "SELECT id FROM USER WHERE nick=?");
+  sqlite3_prepare_v2(db, sqlbuffer, strlen(sqlbuffer) + 1, &stmt, NULL);
+  sqlite3_bind_text(stmt, 1, nick, strlen(nick), SQLITE_STATIC);
+  rc = sqlite3_step(stmt);
+  if (rc == SQLITE_ROW) {
+    int nid = sqlite3_column_int(stmt, 0);
+    printf("%s ID is %d\n", nick, nid);
+  } else {
+    printf("Unable to locate record.\n");
+  }
+  sqlite3_finalize(stmt);
+
+  sqlite3_close(db);
+}
+
+int main(int argc, char *argv[]) {
+  printf("Welcome.\n");
+
+  datado();
+
+  return 0;
+}

BIN
test.db