Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8168291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:28:30+00:00 2026-06-06T20:28:30+00:00

I am having list of char array by that I want to store all

  • 0

I am having list of char array by that I want to store all items in SQLite database , my question how to store string array values in an SQLite database where my string array is dynamically changeable because string array values are coming from server.

here i am done sqlite3 Insert item through Passing parameter , I want to just use this database in dynamically.. Can any guys give me any idea.
I am new to C++ development:

#include <iostream>
using namespace std;
#include "sqlite3.h"
#include<string.h>
#define STRING_MAX 32

typedef struct metadata_t {
    char userid[256];
    char firstname[256];
    char lastname[256];
    char username[256];
    char password[256];
    char email[256];
    char userphone[256];
    char time[256];
} metadata_t;

void insertdata1(metadata_t * data);

int main(int argc, const char *argv[])
{

    struct metadata_t *data;
    cin >> data->userid;
    cin >> data->firstname;
    cin >> data->lastname;
    cin >> data->username;
    cin >> data->password;
    cin >> data->email;
    cin >> data->userphone;
    cin >> data->time;

    insertdata1(data);

}

void insertdata1(metadata_t * data)
{
    sqlite3 *db;
    sqlite3_open("test1.db", &db);

    string createQuery =
        "CREATE TABLE IF NOT EXISTS items (userid INTEGER PRIMARY KEY, firstname TEXT,lastname TEXT,username TEXT,password TEXT,email text,userphone INTEGER, "
        "time TEXT NOT NULL DEFAULT (NOW()));";
    sqlite3_stmt *createStmt;
    cout << "Creating Table Statement" << endl;
    sqlite3_prepare(db, createQuery.c_str(), createQuery.size(),
            &createStmt, NULL);
    cout << "Stepping Table Statement" << endl;
    if (sqlite3_step(createStmt) != SQLITE_DONE)
        cout << "Didn't Create Table!" << endl;

    char *a = "(";
    char *d = ")";
    char *b = "'";
    char *c = ",";
    char str1[1000];
    char *str2 = "";
    char *g = ";";
    strcpy(str1,
           "INSERT INTO items (userid,firstname,lastname,username,password,email,userphone,time)VALUES");

    strcat(str1, a);

    strcat(str1, b);
    strcat(str1, data->userid);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->firstname);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->lastname);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->username);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->password);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->email);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->userphone);
    strcat(str1, b);
    strcat(str1, c);
    strcat(str1, b);
    strcat(str1, data->time);
    strcat(str1, b);
    strcat(str1, d);
    strcat(str1, g);

    std::string insertQuery = str1; // WORKS!
    sqlite3_stmt *insertStmt;
    cout << "Creating Insert Statement" << endl;
    sqlite3_prepare(db, insertQuery.c_str(), insertQuery.size(),
            &insertStmt, NULL);
    cout << "Stepping Insert Statement" << endl;
    if (sqlite3_step(insertStmt) != SQLITE_DONE)
        cout << "Didn't Insert Item!" << endl;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T20:28:31+00:00Added an answer on June 6, 2026 at 8:28 pm

    I suggest to use bind variables – they make your code cleaner and more protected against input errors and even SQL injections.

    There are also good habits that won’t hurt:

    • check return value after every call to sqlite3_* functions and write output of sqlite3_errmsg() to stderr (=cerr) or to a log file in case of error;

    • cleanup after we’re done by calling sqlite3_finalize() on every prepared statement when it’s no longer used.

    Not following these practices will make your karma suffer when someone else uses or debugs your code later.

    So, here is the suggested insertdata1() (I hope, it’s more clean and readable now):

    // some basic error handling
    void exit_with_sqlite_error(sqlite3 *db, const char* file, int line)
    {
    cerr << "Error: " << sqlite3_errmsg(db) << " at " << file << ":" << line << endl;
    exit(1);
    }
    
    // helps to get exact location of an error 
    #define EXIT_WITH_SQLITE_ERR(db) \
    exit_with_sqlite_error(db, __FILE__, __LINE__)
    
    void insertdata1(metadata_t *data)
    {
    sqlite3 *db;
    sqlite3_open("test1.db", &db);
    
    string createQuery = 
        "CREATE TABLE IF NOT EXISTS items ("
        "userid INTEGER PRIMARY KEY,'
        "firstname TEXT,"
        "lastname TEXT,"
        "username TEXT,"
        "password TEXT,'
        "email TEXT,"
        "userphone INTEGER,"
        "time TEXT NOT NULL DEFAULT (NOW()));";
    
    cout << "Creating Table Statement" << endl;
    int rc = sqlite3_exec(db, createQuery.c_str(), NULL, NULL, NULL);
    if (rc != SQLITE_OK) 
        EXIT_WITH_SQLITE_ERR(db);
    
    sqlite3_stmt *insertStmt = NULL;
    rc = sqlite3_prepare_v2(db, 
        "INSERT INTO items "
        "(userid,firstname,lastname,username,password,email,userphone,time)"
        "VALUES"
        "(:userid,:firstname,:lastname,:username,:password,:email,:userphone,:time);",
        -1, &insertStmt, NULL);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    
    rc = sqlite3_bind_int(insertStmt, 1, atoi(data->userid));
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 2, data->firstname, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 3, data->lastname, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 4, data->username, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 5, data->password, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 6, data->email, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_int(insertStmt, 7, atoi(data->userphone));
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    rc = sqlite3_bind_text(insertStmt, 8, data->time, -1, SQLITE_STATIC);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    
    rc = sqlite3_step(insertStmt);
    if (rc != SQLITE_DONE && rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    
    sqlite3_finalize(insertStmt);
    
    // call to sqlite3_close() helps to find resource leaks, because it fails
    // if you have obvious leaks, such as prepared statements not finalized
    rc = sqlite3_close(db);
    if (rc != SQLITE_OK) EXIT_WITH_SQLITE_ERR(db);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem and can't seem to find the solution.. int linearSearch(nodeptr list,char
I was wanting to create a 2 dimensional string array that had dimensions string[5][*]
Im having trouble with a constructor for a linked list. it takes a string
I have a C struct that basically contains two 2D char arrays called List.
Having a list of data object and something visual to represent each, where would
Im having a list of student names in a table , in the same
Consider having a list - Ferrari, Mclaren, Red Bull on A2, A5, A8 cells,
I am having a list like <li style=display: list-item; id=listChoices> <label class=topspace>Enter the Choices</label>
I am actually having a list of text boxes. I am using below code
I m having a ordered list having the structure <ol> <li> </li> <li> </li>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.