HSchmale
HSchmale

Reputation: 1929

SQLITE_MISUSE in prepared statement

I'm getting a SQLITE_MISUSE error on the following code, and I am wondering if it might be caused by having the table name be a bound parameter? What are some different causes of SQLITE_MISUE?

const char sqlNeuralStateInsert[] =
    "INSERT INTO ?1(LAYER_ID, NEURON_ID, INPUT_ID, VALUE)"
    "VALUES(?2, ?3, ?4, ?5);";
sqlite3_stmt* stmt1;
rc = sqlite3_prepare_v2(db, sqlNeuralStateInsert, -1, &stmt1, NULL);
if(rc){
    //!< Failed to prepare insert statement
}
sqlite3_bind_text(stmt1, 1, this->getNName().c_str(), -1, SQLITE_STATIC); 
for(uint32_t i = 0; i < m_nlayers; i++){
    sqlite3_bind_int(stmt1, 2, i); // Layer id
    for(uint32_t j = 0; j < m_layers[i]->getNeuronCount(); j++){
        std::vector<double> weights = m_layers[i]->getWeights(j);
        sqlite3_bind_int(stmt1, 3, j); // Neuron id
        for(uint32_t k = 0; k < weights.size(); k++){
            sqlite3_bind_int(stmt1, 4, k);
            sqlite3_bind_double(stmt1, 5, weights[k]);
            rc = sqlite3_step(stmt1);
            printf("%d\n", rc);
        }
    }
}
sqlite3_finalize(stmt1);

Upvotes: 1

Views: 1828

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385144

You're right; you cannot bind the table name:

Generally one cannot use SQL parameters/placeholders for database identifiers (tables, columns, views, schemas, etc.) or database functions (e.g., CURRENT_DATE), but instead only for binding literal values.

You could have trivially tested this hypothesis by hard-coding the table name.

Upvotes: 1

Related Questions