mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 02:50:46 -06:00
Make it possible to add rows to a WITHOUT rowid table
This commit allows you to add new rows to a table without rowid column. The main problem here is that SQLite won't create the next value for the primary key column itself, so we have to do that instead. See issue #51.
This commit is contained in:
@@ -156,28 +156,29 @@ QPair<Table, bool> Table::parseSQL(const QString &sSQL)
|
||||
|
||||
return qMakePair(Table(""), false);
|
||||
}
|
||||
QString Table::emptyInsertStmt() const
|
||||
QString Table::emptyInsertStmt(int pk_value) const
|
||||
{
|
||||
QString stmt = QString("INSERT INTO `%1`").arg(m_name);
|
||||
|
||||
QStringList vals;
|
||||
QStringList fields;
|
||||
foreach(FieldPtr f, m_fields) {
|
||||
if(f->notnull())
|
||||
if( f->primaryKey() && f->isInteger() )
|
||||
{
|
||||
fields << f->name();
|
||||
if( f->primaryKey() && f->isInteger() )
|
||||
{
|
||||
|
||||
if(pk_value != -1)
|
||||
vals << QString::number(pk_value);
|
||||
else
|
||||
vals << "NULL";
|
||||
} else {
|
||||
if(f->isInteger())
|
||||
vals << "0";
|
||||
else
|
||||
vals << "''";
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
} else if(f->notnull()) {
|
||||
fields << f->name();
|
||||
|
||||
if(f->isInteger())
|
||||
vals << "0";
|
||||
else
|
||||
vals << "''";
|
||||
} else {
|
||||
// don't insert into fields with a default value
|
||||
// or we will never see it.
|
||||
if(f->defaultValue().length() == 0)
|
||||
@@ -228,7 +229,7 @@ QString Table::sql() const
|
||||
sql += "\n)";
|
||||
|
||||
// without rowid
|
||||
if(m_rowidColumn != "_rowid_")
|
||||
if(isWithoutRowidTable())
|
||||
sql += " WITHOUT ROWID";
|
||||
|
||||
return sql + ";";
|
||||
|
||||
Reference in New Issue
Block a user