Rework the kinda broken add record button

Now it should always be possible to add a new record,
no matter how the table is defined or what the new data default is.
The default new data setting will be removed in the next commit.
This commit is contained in:
Peinthor Rene
2013-03-24 21:26:01 +01:00
parent d08f57c772
commit d7d6a861b2
3 changed files with 23 additions and 27 deletions

View File

@@ -367,11 +367,14 @@ void MainWindow::closeEvent( QCloseEvent* event )
void MainWindow::addRecord()
{
if (db.addRecord()){
if (db.addRecord(db.curBrowseTableName))
{
populateTable(db.curBrowseTableName);
//added record will be the last one in view
updateTableView(db.getRecordCount()-1);
}else{
}
else
{
QMessageBox::information( this, QApplication::applicationName(),
tr("Error adding record, make sure a table is selected.\n\n"
"If the table contain fields declared as NOT NULL\n"

View File

@@ -378,35 +378,28 @@ bool DBBrowserDB::executeMultiSQL(const QString& statement, bool dirty, bool log
return true;
}
bool DBBrowserDB::addRecord ( )
bool DBBrowserDB::addRecord(const QString& sTableName)
{
char *errmsg;
if (!hasValidBrowseSet) return false;
if (!isOpen()) return false;
bool ok = false;
int fields = browseFields.count();
QString emptyvalue = curNewData;
QString statement = QString("INSERT INTO `%1` VALUES(").arg(curBrowseTableName);
for ( int i=1; i<=fields; ++i ) {
statement.append(emptyvalue);
if (i<fields) statement.append(", ");
}
statement.append(");");
lastErrorMessage = QObject::tr("no error");
if (_db){
logSQL(statement, kLogMsg_App);
setRestorePoint();
if (SQLITE_OK==sqlite3_exec(_db,statement.toUtf8(),NULL,NULL, &errmsg)){
ok=true;
//int newrowid = sqlite3_last_insert_rowid(_db);
} else {
lastErrorMessage = QString::fromUtf8(errmsg);
qCritical() << "addRecord: " << lastErrorMessage;
}
}
// add record is seldom called, for now this is ok
// but if we ever going to add a lot of records
// we should cache the parsed tables somewhere
sqlb::Table table = sqlb::Table::parseSQL(getTableSQL(sTableName));
QString sInsertstmt = table.emptyInsertStmt();
return ok;
lastErrorMessage = "";
logSQL(sInsertstmt, kLogMsg_App);
setRestorePoint();
if (SQLITE_OK != sqlite3_exec(_db, sInsertstmt.toUtf8(), NULL, NULL, &errmsg))
{
lastErrorMessage = QString::fromUtf8(errmsg);
qCritical() << "addRecord: " << lastErrorMessage;
return false;
}
return true;
}
bool DBBrowserDB::deleteRecord( int wrow)

View File

@@ -99,7 +99,7 @@ public:
*/
QString getTableSQL(const QString& sTable);
void updateSchema() ;
bool addRecord();
bool addRecord(const QString& sTableName);
bool deleteRecord(int wrow);
bool updateRecord(int wrow, int wcol, const QByteArray& wtext);
bool browseTable( const QString & tablename, const QString& orderby = "rowid" );