Seite 1 von 1

QString lastIndexOf(" "); (space) keine richtige

Verfasst: 4. Mai 2006 10:43
von patrik08
da sqlite keinen richtigen ALTER TABLE hat um ein index INTEGER PRIMARY KEY, ( hat schon aber muss close e open db) hinzuzufuegen hat muss ich ....

dies erreichen ....

Code: Alles auswählen

/*
 result to have a new cool  index (id INTEGER PRIMARY KEY) and not restart db!
CREATE TEMPORARY TABLE TEMP_TABLE(Male, Sente);
INSERT INTO TEMP_TABLE SELECT Male, Sente FROM Mustermann;
DROP TABLE Mustermann;
CREATE TABLE Mustermann (id INTEGER PRIMARY KEY, Male TEXT, Sente TEXT);
INSERT INTO Mustermann(Male, Sente) SELECT Male, Sente FROM TEMP_TABLE;
DROP TABLE TEMP_TABLE;
*/
table name ist bekannt nr cool & row und das sql original insert..

also sql CREATE TABLE Mustermann (Male TEXT,Sente TEXT)

und nun den index appenden.... ich splitte die feld namen alles in ()


doch die posizion zu finden vom ersten " " space ist nicht leicht...
um and den richtigen getrimmte feldnamen zu kommen
da bei dieser operation keine TEXT oder nummer noetig sind ... erst am schluss um die temp table mit index zu bauen...

wie finde ich die position vom ersten space im QStringList allnamelist
um sie dan wieder zusammenzufuegen feldname,***,

Code: Alles auswählen

void Include_Table::CreateIndexTable()
{
/*  structuresql = GetStructureTable(tablesql); */
/* structuresql = CREATE TABLE Mustermann (Male  TEXT,Sente TEXT) */
QString only_name_one; 
QString only_name_all;
int numer;
/* clean super */
QString struct_table_original = structuresql;
QString tmp_table_a = "CREATE TEMPORARY TABLE TEMP_TABLE";
QString tmp_table_end = "CREATE TABLE "+tablename;
/* clean super */
QString drop_table = "DROP TABLE "+tablename;

QString      table_para = structuresql.replace(tmp_table_end, QString(""));
int          resize = table_para.lastIndexOf(")");
             table_para = table_para.left(resize);  /* remove () */
             table_para = table_para.right(resize - 2); /* remove () and space */
    
/* clean super */
QString command_end_new_table =tmp_table_end +" (" + "id INTEGER PRIMARY KEY," + table_para + ")"; 
QStringList  allnamelist = table_para.split(","); 
    
    for (int i = 0; i < allnamelist.size(); ++i) {
        only_name_one = XML_utf8(QString( allnamelist.at(i) ));
        numer = only_name_one.lastIndexOf(" ");
        only_name_one.left(numer);
                  if (loop !=allnamelist.size() - 1) {
                    only_name_all = only_name_all+QString( "%1," ).arg( only_name_one ); 
                  } else {
                   only_name_all = only_name_all+QString( "%1" ).arg( only_name_one );   
                  }
         numer =0;
         only_name_one ="";
    }
    
    

msgb->information( this , "File Error!","result ="+table_para+" - "+structuresql);
    

}

geloest!

Verfasst: 4. Mai 2006 13:24
von patrik08
habe einfach space mit # ausgetaucht so sind es am schluss sauber getrimmt ... ob blob , text nummer.. ecc...

Code: Alles auswählen

void Include_Table::CreateIndexTable()
{
/*  structuresql = GetStructureTable(tablesql); */
QString only_name_one; 
QString only_name_all;
QString tmp_table_end = "CREATE TABLE "+tablename; 
int numer;
QString struct_table_original = structuresql;
QString      table_para = structuresql.replace(tmp_table_end, QString(""));
int          resize = table_para.lastIndexOf(")");
             table_para = table_para.left(resize);  /* remove () */
             table_para = table_para.right(resize - 2); /* remove () and space */
    
QString sendtolist = table_para;
        sendtolist.replace(QString(" "), QString("#"));
QStringList  allnamelist = sendtolist.split(",");

    for (int i = 0; i < allnamelist.size(); ++i) {
        only_name_one = XML_utf8(QString( allnamelist.at(i) ));
        numer = only_name_one.lastIndexOf("#");
        QString trimm_str = only_name_one.left(numer);
        numer = trimm_str.lastIndexOf("#");
        trimm_str.left(numer);
        trimm_str.replace(QString("#"), QString(""));
                  if (i !=allnamelist.size() - 1) {
                    only_name_all = only_name_all+QString( "%1," ).arg( trimm_str ); 
                  } else {
                   only_name_all = only_name_all+QString( "%1" ).arg( trimm_str );   
                  }
         numer =0;
         only_name_one ="";
         trimm_str = "";
    }

QStringList prepare_query = ( QStringList()
                             << "CREATE TEMPORARY TABLE TEMP_TABLE ("+only_name_all+")"
                             << "INSERT INTO TEMP_TABLE SELECT "+only_name_all+" FROM "+tablename
                             << "DROP TABLE "+tablename
                             << "CREATE TABLE "+tablename+" (id INTEGER PRIMARY KEY,"+table_para+")"
                             << "INSERT INTO "+tablename+"("+only_name_all+") SELECT "+only_name_all+" FROM TEMP_TABLE"
                             << "DROP TABLE TEMP_TABLE" );
    
                 for (int i = 0; i < prepare_query.size(); ++i) {
                    query.exec("BEGIN TRANSACTION");
                    query.exec(QString( prepare_query.at(i) ));
                    query.exec("COMMIT");
                 }
/* reload qtableview*/
}
die sql structur lesen ...

Code: Alles auswählen

QString Gui_Starter::GetStructureTable(QString t)
{
    QString tablequery =QString( "SELECT sql FROM sqlite_master WHERE type!='meta' AND sql NOT NULL AND name='%1' LIMIT 1" ).arg( t );
    QSqlQuery query;
    QString countstr;
    QString structure ="";
        query.exec(tablequery);
        query.first();
        structure = query.value(0).toString();
          if(structure.size() > 0 ) {
           return structure;
          }
          
return structure;
/*  on return check size....*/
}


um zu wissen ob index da ist oder nicht...

structuresql = GetStructureTable(tablesql);
bool have_index = structuresql.contains("integer primary key", Qt::CaseInsensitive);