Index: src/build.c ================================================================== --- src/build.c +++ src/build.c @@ -2629,20 +2629,11 @@ iSorter = pParse->nTab++; sqlite3VdbeAddOp4(v, OP_SorterOpen, iSorter, 0, 0, (char*)pKey, P4_KEYINFO); /* Open the table. Loop through all rows of the table, inserting index ** records into the sorter. */ - if( HasRowid(pTab) ){ - sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); - }else{ - Index *pPk = sqlite3PrimaryKeyIndex(pTab); - assert( pPk!=0 ); - assert( pPk->tnum=pTab->tnum ); - sqlite3VdbeAddOp4(v, OP_OpenRead, iTab, pPk->tnum, iDb, - (char*)sqlite3IndexKeyinfo(pParse, pPk), - P4_KEYINFO_HANDOFF); - } + sqlite3OpenTable(pParse, iTab, iDb, pTab, OP_OpenRead); addr1 = sqlite3VdbeAddOp2(v, OP_Rewind, iTab, 0); regRecord = sqlite3GetTempReg(pParse); sqlite3GenerateIndexKey(pParse, pIndex, iTab, regRecord, 1, &iPartIdxLabel); sqlite3VdbeAddOp2(v, OP_SorterInsert, iSorter, regRecord); Index: src/insert.c ================================================================== --- src/insert.c +++ src/insert.c @@ -13,11 +13,17 @@ ** to handle INSERT statements in SQLite. */ #include "sqliteInt.h" /* -** Generate code that will open a table for reading. +** Generate code that will open table pTab for reading or writing +** on cursor iCur. +** +** Always acquire a table lock. Always do the open for rowid tables. +** For WITHOUT ROWID tables, only do read opens, and then open the +** PRIMARY KEY index, not the main table, since the main table doesn't +** exist. */ void sqlite3OpenTable( Parse *p, /* Generate code into this VDBE */ int iCur, /* The cursor number of the table */ int iDb, /* The database index in sqlite3.aDb[] */ @@ -28,12 +34,19 @@ assert( !IsVirtual(pTab) ); v = sqlite3GetVdbe(p); assert( opcode==OP_OpenWrite || opcode==OP_OpenRead ); sqlite3TableLock(p, iDb, pTab->tnum, (opcode==OP_OpenWrite)?1:0, pTab->zName); if( HasRowid(pTab) ){ - sqlite3VdbeAddOp3(v, opcode, iCur, pTab->tnum, iDb); - sqlite3VdbeChangeP4(v, -1, SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32); + sqlite3VdbeAddOp4(v, opcode, iCur, pTab->tnum, iDb, + SQLITE_INT_TO_PTR(pTab->nCol), P4_INT32); + VdbeComment((v, "%s", pTab->zName)); + }else if( opcode==OP_OpenRead ){ + Index *pPk = sqlite3PrimaryKeyIndex(pTab); + assert( pPk!=0 ); + assert( pPk->tnum=pTab->tnum ); + sqlite3VdbeAddOp4(v, opcode, iCur, pPk->tnum, iDb, + (char*)sqlite3IndexKeyinfo(p, pPk), P4_KEYINFO_HANDOFF); VdbeComment((v, "%s", pTab->zName)); } } /* Index: src/where.c ================================================================== --- src/where.c +++ src/where.c @@ -5953,11 +5953,11 @@ && (wctrlFlags & WHERE_OMIT_OPEN_CLOSE)==0 ){ int op = pWInfo->okOnePass ? OP_OpenWrite : OP_OpenRead; sqlite3OpenTable(pParse, pTabItem->iCursor, iDb, pTab, op); testcase( !pWInfo->okOnePass && pTab->nCol==BMS-1 ); testcase( !pWInfo->okOnePass && pTab->nCol==BMS ); - if( !pWInfo->okOnePass && pTab->nColokOnePass && pTab->nColcolUsed; int n = 0; for(; b; b=b>>1, n++){} sqlite3VdbeChangeP4(v, sqlite3VdbeCurrentAddr(v)-1, SQLITE_INT_TO_PTR(n), P4_INT32);