Can't execute all query!



  • Hi
    I'm using sqlite , and want to run this query from sql file:
    @
    CREATE TABLE "dic2" (
    "name" TEXT,
    "alias" TEXT
    );
    insert into dic2 ("name", "alias") values ('12', '12');
    insert into dic2 ("name", "alias") values ('123', '123');
    insert into dic2 ("name", "alias") values ('123123', 'sdasdasd');
    insert into dic2 ("name", "alias") values ('123', 'asdasda');
    insert into dic2 ("name", "alias") values ('asd', 'sasdasd');
    @

    table is made but, can't insert values into table! why?



  • Did you get any errors?



  • No , it works!
    but just can create table and can't insert values!



  • That's strange. Could you please share your source code. What is your OS?



  • I'm using Windows 7 64bits.
    and this is code :
    @
    QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
    "",
    tr("Files (.)"));
    QFile file(fileName);
    if(!file.open(QIODevice::ReadOnly)) {
    QMessageBox::information(0, "error", file.errorString());
    }
    QString line;
    QTextStream in(&file);
    line = in.readAll();

    if (mw->dbconnect())
    {
    QSqlQuery query(line);
    }
    @



  • The SQL seems OK but try to execute it via "the SQLite command-line shell for accessing and modifying SQLite databases for Windows":http://www.sqlite.org/download.html

    Please also consider the option to execute each of your SQL statement is a separate QSqlQuery in your source code.



  • I think the problem is in SQL code. When you declare table and columns names you should not use the "". Moreover, I never saw the TEXT declaration. Try this code:
    @
    CREATE TABLE dic2 ( name CHAR(10), alias CHAR(10) );

    insert into dic2 (name, alias) values ('12', '12');
    insert into dic2 (name, alias) values ('123', '123');
    insert into dic2 (name, alias) values ('123123', 'sdasdasd');
    insert into dic2 (name, alias) values ('123', 'asdasda');
    insert into dic2 (name, alias) values ('asd', 'sasdasd');
    @

    Before writing SQL statements into Qt applications is always best to test them in a SQL console.
    Hope it helps.



  • [quote author="Seba84" date="1342383542"]I think the problem is in SQL code. When you declare table and columns names you should not use the "". Moreover, I never saw the TEXT declaration. [/quote]

    Good point I didn't spot the quotes at the insert statement. TEXT is SQLite specific data type. Check "SQLite documentation":http://www.sqlite.org/datatype3.html for details.



  • Hi Seba84,My sql code is works right in console,but in qt , i don't know why can't execute complete of queries!



  • but,its OK!
    because qt can execute one query per action!
    i should convert my sql code to one code such as this :
    @
    INSERT INTO cds (titel, interpret, jahr, id) VALUES
    ('Beauty', 'Ryuichi Sakamoto', 1990, 1),
    ('Goodbye Country (Hello Nightclub)', 'Groove Armada', 2001, 4),
    ('Glee', 'Bran Van 3000', 1997, 5);
    @



  • Yes, Qt only accepts a single SQL statement at a time, and I doubt your last attempt is valid SQL. You'll just have to execute 6 separate statements, and you'll be fine. You could wrap them in a single transaction to keep them as a unit.



  • yes,thanks all and thanks to Andre for complete detail. ;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.