Mysql数据库查询的记录用链表保存 mysql数据库WINDOWS平台下代码移植到LINUX下的问题

 

//创建了一个空链表
pnode create_node(void)
{
    pnode l = (pnode) malloc(sizeof(linknode));
    if (NULL == l)
    {
        printf("malloc fail\n");
        return NULL;
    }
    l->next = NULL;
    return l;
}


bool insert_node(pnode h,Node x)
{
    pnode p = h, q = NULL;
    
        q = (pnode)malloc(sizeof(linknode));
        if (NULL == q)
        {
            printf("malloc fail\n");
            return 1;
        }
        q->node = x;
        q->next = p->next;
        p->next = q;
        return 0;

}

void delete_node(pnode h)
{
    pnode p = h;
        pnode q;
        q = p->next;
    while(q)
    {
        p->next = q->next;
        free(q);
        q = p->next;
    }        
}
bool displaynode(pnode h)
{
    bool i = 1;
    pnode p = h->next;
    while (p)
    {
        bool i = 0;
        qDebug("%d %s %d ",p->node.node_id,p->node.ipaddress,p->node.sourceCardNum);
        p = p->next;
    }
    //    qDebug()<<endl;
    return i;
}

上面是链表操作

bool KLPJ::InsertNode(Node *pnode)
{
    QSqlQuery pquery;
    QTextStream cout(stdout,  QIODevice::WriteOnly);

    QString string;
    string = QString( "insert into Node values('%1','%2','%3')" )
               .arg( pnode->node_id )
               .arg( pnode->ipaddress )
               .arg( pnode->sourceCardNum );

    cout<<string<<endl;
    if(pquery.exec(string))
    {
        qDebug()<<"KLPJ_InsertNode sucess!\n";
        return 0;
    }
    else
    {
        qDebug()<<"KLPJ_InsertNode error!\n";
        return 1;
    }

}

bool KLPJ::DeleteNode(int node_id)
{
    QSqlQuery pquery;
    QTextStream cout(stdout,  QIODevice::WriteOnly);
    QString string;
    string = QString( "delete from Node where node_id = '%1'" )
               .arg( node_id );

    cout<<string<<endl;
    if(pquery.exec(string))
    {
        qDebug()<<"KLPJ_DeleteNode sucess!\n";
        return 0;
    }
    else
    {
        qDebug()<<"KLPJ_DeleteNode error!\n";
        return 1;
    }

}

bool KLPJ::EditNode(int node_id,char ipaddress[20],int sourceCardNum)
{
    QSqlQuery pquery;
    QTextStream cout(stdout,  QIODevice::WriteOnly);
    QString string;
    string = QString( "update Node set ipaddress = '%1',sourceCardNum = '%2' where node_id = '%3'" )
               .arg( ipaddress )
               .arg( sourceCardNum )
               .arg( node_id );

    cout<<string<<endl;
    if(pquery.exec(string))
    {
        qDebug()<<"KLPJ_EditNode sucess!\n";
        return 0;
    }
    else
    {
        qDebug()<<"KLPJ_EditNode error!\n";
        return 1;
    }

}

bool KLPJ::SelectNode(pnode h)
{
    QTextStream cout(stdout,  QIODevice::WriteOnly);
        bool i = 1;
        QSqlQuery query;
        query.exec("select * from Node");
        while(query.next())
        {
            i = 0;
           qDebug()<< query.value(0).toInt()
                   << query.value(1).toString()
                   << query.value(2).toInt();
           Node x;
           qstrcpy(x.ipaddress ,(const char *)query.value(1).toString().toStdString().data()); 
           x.node_id = query.value(0).toInt();
           x.sourceCardNum = query.value(2).toInt();
           insert_node(h,x);
        }
    return i;
}

bool KLPJ::SelectNode(pnode h, int node_id)
{
    QTextStream cout(stdout,  QIODevice::WriteOnly);
        bool i = 1;
        QSqlQuery query;
        QString string;
        string = QString("select * from Node where node_id = '%1'")
                .arg(node_id);
        query.exec(string);
        while(query.next())
        {
            i = 0;
           qDebug()<< query.value(0).toInt()
                   << query.value(1).toString()
                   << query.value(2).toInt();
              Node x;
           qstrcpy(x.ipaddress ,(const char *)query.value(1).toString().toStdString().data()); 
           x.node_id = query.value(0).toInt();
           x.sourceCardNum = query.value(2).toInt();
           insert_node(h,x);

        }
    return i;
}
bool KLPJ::SelectNode(pnode h, char cond[20],char temp[20])
{
    QTextStream cout(stdout,  QIODevice::WriteOnly);
        bool i = 1;
        QSqlQuery query;
        QString string;
        string = QString("select * from Node where %1 = '%2'")
                .arg(cond)
                .arg(temp);
        query.exec(string);
        while(query.next())
        {
            i = 0;
           qDebug()<< query.value(0).toInt()
                   << query.value(1).toString()
                   << query.value(2).toInt();
           Node x;
           qstrcpy(x.ipaddress ,(const char *)query.value(1).toString().toStdString().data()); 
           x.node_id = query.value(0).toInt();
           x.sourceCardNum = query.value(2).toInt();
           insert_node(h,x);

        }
    return i;
}

上面是数据库操作

class Node
{
public:
        int  node_id;               
        char ipaddress[20];    
        int  sourceCardNum;    
};

node类

typedef struct _node
{
    Node node;
    struct _node *next;
}linknode, *pnode;

链表定义

pnode create_node(void);
extern bool insert_node(pnode h,Node node);
extern void delete_node(pnode h);
extern bool displaynode(pnode h);

函数声明

pnode ph = create_node();//linknode  test
        qDebug()<<"select*********************************************************";
        k.SelectNode(ph);
        k.SelectNode(ph,1);
        k.SelectNode(ph,"node_id","1");
        qDebug()<<"link***********************************************************";
        displaynode(ph);
        delete_node(ph);
        displaynode(ph);

main中测试结果

当把 代码移植到LINUX下时数据库问题出现,由于WINDOWS下mysql的数据库名和表名都是小写,而在LINUX中分大小写所以 建表和库名的时候建议都弄成小写字符,便于移植

posted @ 2014-05-28 17:25  这是一个大大的屌丝  阅读(666)  评论(0)    收藏  举报