Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I verify if database is set
    primarykey
    data
    text
    <p>I called <code>mysql_select_db()</code> and it returned no errors, but when I called <code>mysql_query()</code> I got the following error: </p> <blockquote> <p>No database selected.</p> </blockquote> <p>I found a parameter of MYSQL struct. It is some char* calls db. What does it represent? <code>mysql_select_db()</code> does changes this parameter?</p> <p>Thanks.</p> <p>Edit:</p> <p>My main():</p> <pre><code>#include t-mysql.h ... T_MYSQL *db = (T_MYSQL*) malloc(sizeof(T_MYSQL)); strcpy(db-&gt;database_name,mysql_database_name); strcpy(db-&gt;password,mysql_password); strcpy(db-&gt;root_password,mysql_root_password); strcpy(db-&gt;server,mysql_server); strcpy(db-&gt;table_name,mysql_table_name); strcpy(db-&gt;user,mysql_user); strcpy(db-&gt;query_create_db,query_create_db); strcpy(db-&gt;query_create_table,query_create_table); t_mysql_init(db); t_mysql_connect(db); t_mysql_createdb(db); t_mysql_setdb(db); t_mysql_createtable(db); ... </code></pre> <p>t-mysql.h:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;mysql.h&gt; #include &lt;my_global.h&gt; #ifndef T_MYSQL_H_ #define T_MYSQL_H_ #define MAX_NAME_LENGTH 128 #define MAX_QUERY_LENGTH 1024 typedef struct { MYSQL *con; char password[MAX_NAME_LENGTH]; char root_password[MAX_NAME_LENGTH]; char user[MAX_NAME_LENGTH]; char server[MAX_NAME_LENGTH]; char database_name[MAX_NAME_LENGTH]; char table_name[MAX_NAME_LENGTH]; char query_create_table[MAX_QUERY_LENGTH]; char query_create_db[MAX_QUERY_LENGTH]; char query_data[MAX_QUERY_LENGTH]; }T_MYSQL; void finish_with_error(MYSQL *con); int t_mysql_init(T_MYSQL *mysql); int t_mysql_connect(T_MYSQL *mysql); int t_mysql_disconnect(T_MYSQL *mysql); int t_mysql_createdb(T_MYSQL *mysql); int t_mysql_createtable(T_MYSQL *mysql); int t_mysql_setdb(T_MYSQL *mysql); int t_mysql_insertdata(T_MYSQL *mysql); int t_mysql_quickinsert(T_MYSQL *mysql); #endif /* T_MYSQL_H_ */ </code></pre> <p>t-mysql.c:</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;mysql.h&gt; #include &lt;my_global.h&gt; #include "t-mysql.h" void finish_with_error(MYSQL *con){ fprintf(stderr, "%s\n", mysql_error(con)); mysql_close(con); //close connection to database } int t_mysql_init(T_MYSQL *mysql){ mysql-&gt;con = mysql_init(NULL); if (mysql-&gt;con == NULL){ finish_with_error(mysql-&gt;con); return 0; } return 1; } int t_mysql_connect(T_MYSQL *mysql){ // connect to MySQL if (mysql_real_connect(mysql-&gt;con, mysql-&gt;server, mysql-&gt;user, mysql-&gt;password,NULL, 0, NULL, 0) == NULL){ finish_with_error(mysql-&gt;con); return 0; } return 1; } int t_mysql_disconnect(T_MYSQL *mysql){ mysql_close(mysql-&gt;con); if ( mysql-&gt;con == NULL){ finish_with_error(mysql-&gt;con); return 0; } return 1; } int t_mysql_createdb(T_MYSQL *mysql){ t_mysql_disconnect(mysql); //disconnect normal user //connect as root t_mysql_init(mysql); if (mysql_real_connect(mysql-&gt;con, mysql-&gt;server, "root", mysql-&gt;root_password,NULL, 0, NULL, 0) == NULL){ finish_with_error(mysql-&gt;con); return 0; } // create DB, if it doesn't exist if (mysql_select_db(mysql-&gt;con, mysql-&gt;database_name)){ if (mysql_query(mysql-&gt;con, mysql-&gt;query_create_db)){ finish_with_error(mysql-&gt;con); return 0; } else { fprintf(stdout,"Database %s created\n",mysql-&gt;database_name); } }else{ fprintf(stdout,"Database %s exists\n",mysql-&gt;database_name); } //disconnect root user t_mysql_disconnect(mysql); //connect as normal user t_mysql_init(mysql); t_mysql_connect(mysql); return 1; } int t_mysql_setdb(T_MYSQL *mysql){ if (mysql_select_db(mysql-&gt;con, mysql-&gt;database_name) == NULL){ finish_with_error(mysql-&gt;con); return 0; } return 1; } int t_mysql_createtable(T_MYSQL *mysql){ //create table: if(mysql_query(mysql-&gt;con, mysql-&gt;query_create_table)){ finish_with_error(mysql-&gt;con); return 0; } return 1; } int t_mysql_insertdata(T_MYSQL *mysql){ if (mysql_query(mysql-&gt;con, mysql-&gt;query_data)) { finish_with_error(mysql-&gt;con); return 0; } return 1; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload