File: mysqlcursor.cpp

package info (click to toggle)
kdb 3.2.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,276 kB
  • sloc: cpp: 38,360; yacc: 940; python: 779; sh: 711; ansic: 440; lex: 367; xml: 182; sql: 51; makefile: 10
file content (140 lines) | stat: -rw-r--r-- 4,491 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* This file is part of the KDE project
   Copyright (C) 2003-2010 Jarosław Staniek <staniek@kde.org>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include <QDebug>
#include <kcomponentdata.h>

#include <KDbDriverManager>
#include <KDbDriver>
#include <KDbConnection>
#include <KDbCursor>

int main(int argc, char * argv[])
{
    KComponentData componentData("newapi");
    KDbDriverManager manager;
    QStringList driverIds = manager.driverIds();
    qDebug() << "DRIVERS: ";
    for (QStringList::ConstIterator it = driverIds.constBegin(); it != driverIds.constEnd() ; ++it)
        qDebug() << *it;
    if (manager.error()) {
        qDebug() << manager.errorMsg();
        return 1;
    }

    //get driver
    KDbDriver *driver = manager.driver("mySQL");
    if (manager.error()) {
        qDebug() << manager.errorMsg();
        return 1;
    }

    //connection data that can be later reused
    KDbConnectionData conn_data;

    conn_data.userName = "root";
    if (argc > 1)
        conn_data.password = argv[1];
    else
        conn_data.password = "mysql";
    conn_data.hostName = "localhost";

    KDbConnection *conn = driver->createConnection(conn_data);
    if (driver->error()) {
        qDebug() << driver->errorMsg();
        return 1;
    }
    if (!conn->connect()) {
        qDebug() << conn->errorMsg();
        return 1;
    }

    if (!conn->useDatabase("test")) {
        qDebug() << "use db:" << conn->errorMsg();
        return 1;
    }

    qDebug() << "Creating first cursor";
    KDbCursor *c = conn->executeQuery("select * from Applications");
    if (!c) qDebug() << conn->errorMsg();
    qDebug() << "Creating second cursor";
    KDbCursor *c2 = conn->executeQuery("select * from Applications");
    if (!c2) qDebug() << conn->errorMsg();

    QStringList l = conn->databaseNames();
    if (l.isEmpty()) qDebug() << conn->errorMsg();
    qDebug() << "Databases:";
    for (QStringList::ConstIterator it = l.constBegin(); it != l.constEnd() ; ++it)
        qDebug() << *it;

    if (c) {
        while (c->moveNext()) {
            qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
            qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
        }
        qDebug() << "KDbCursor error:" << c->errorMsg();
    }
    if (c2) {
        while (c2->moveNext()) {
            qDebug() << "Cursor2: Value(0)" << c2->value(0).toString();
            qDebug() << "Cursor2: Value(1)" << c2->value(1).toString();
        }
    }
    if (c) {
        qDebug() << "KDbCursor::prev";
        while (c->movePrev()) {
            qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
            qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();

        }
        qDebug() << "up/down";
        c->moveNext();
        qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
        qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
        c->moveNext();
        qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
        qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
        c->movePrev();
        qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
        qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();
        c->movePrev();
        qDebug() << "KDbCursor: Value(0)" << c->value(0).toString();
        qDebug() << "KDbCursor: Value(1)" << c->value(1).toString();

    }
#if 0
    KDbTable *t = conn->tableSchema("persons");
    if (t)
        t->debug();
    t = conn->tableSchema("cars");
    if (t)
        t->debug();

// conn->tableNames();

    if (!conn->disconnect()) {
        qDebug() << conn->errorMsg();
        return 1;
    }
    debug("before del");
    delete conn;
    debug("after del");
#endif
    return 0;
}