从SQLite表中删除列

我有一个问题:我需要从我的SQLite数据库中删除一列。 我写了这个查询

alter table table_name drop column column_name 

但它不起作用。 请帮帮我。

来自: http : //www.sqlite.org/faq.html :

(11)如何在SQLite的现有表中添加或删除列。

SQLite具有有限的ALTER TABLE支持,您可以使用它将列添加到表的末尾或更改表的名称。 如果您想在表格结构中进行更复杂的更改,则必须重新创build表格。 您可以将现有数据保存到临时表中,删除旧表,创build新表,然后将数据从临时表中复制回来。

例如,假设您有一个名为“t1”的表,其中列名称为“a”,“b”和“c”,并且您希望从此表中删除列“c”。 以下步骤说明了如何做到这一点:

 BEGIN TRANSACTION; CREATE TEMPORARY TABLE t1_backup(a,b); INSERT INTO t1_backup SELECT a,b FROM t1; DROP TABLE t1; CREATE TABLE t1(a,b); INSERT INTO t1 SELECT a,b FROM t1_backup; DROP TABLE t1_backup; COMMIT; 

而不是删除备份表,只需重命名它…

 BEGIN TRANSACTION; CREATE TABLE t1_backup(a,b); INSERT INTO t1_backup SELECT a,b FROM t1; DROP TABLE t1; ALTER TABLE t1_backup RENAME TO t1; COMMIT; 

为了简单起见,为什么不从select语句创build备份表?

 CREATE TABLE t1_backup AS SELECT a, b FROM t1; DROP TABLE t1; ALTER TABLE t1_backup RENAME TO t1; 

http://lists.osgeo.org/pipermail/grass-user/2006-January/031981.html

还有一个名为Sqliteman的工具,它提供了删除列的可视选项。

谢谢,Jignesh

=>使用以下查询直接创build新表:

 CREATE TABLE Table_name (Column_1 text,Column_2 text); 

=>现在使用以下查询从Existing_table中将数据插入到table_name中:

 insert into Table_name (Column_1,Column_2) FROM Existing_Table; 

=>现在通过以下查询删除Existing_table:

 DROP Existing_Table; 

对于SQLite3 c ++:

 void GetTableColNames( tstring sTableName , std::vector<tstring> *pvsCols ) { UASSERT(pvsCols); CppSQLite3Table table1; tstring sDML = StringOps::std_sprintf(_T("SELECT * FROM %s") , sTableName.c_str() ); table1 = getTable( StringOps::tstringToUTF8string(sDML).c_str() ); for ( int nCol = 0 ; nCol < table1.numFields() ; nCol++ ) { const char* pch1 = table1.fieldName(nCol); pvsCols->push_back( StringOps::UTF8charTo_tstring(pch1)); } } bool ColExists( tstring sColName ) { bool bColExists = true; try { tstring sQuery = StringOps::std_sprintf(_T("SELECT %s FROM MyOriginalTable LIMIT 1;") , sColName.c_str() ); ShowVerbalMessages(false); CppSQLite3Query q = execQuery( StringOps::tstringTo_stdString(sQuery).c_str() ); ShowVerbalMessages(true); } catch (CppSQLite3Exception& e) { bColExists = false; } return bColExists; } void DeleteColumns( std::vector<tstring> *pvsColsToDelete ) { UASSERT(pvsColsToDelete); execDML( StringOps::tstringTo_stdString(_T("begin transaction;")).c_str() ); std::vector<tstring> vsCols; GetTableColNames( _T("MyOriginalTable") , &vsCols ); CreateFields( _T("TempTable1") , false ); tstring sFieldNamesSeperatedByCommas; for ( int nCol = 0 ; nCol < vsCols.size() ; nCol++ ) { tstring sColNameCurr = vsCols.at(nCol); bool bUseCol = true; for ( int nColsToDelete = 0; nColsToDelete < pvsColsToDelete->size() ; nColsToDelete++ ) { if ( pvsColsToDelete->at(nColsToDelete) == sColNameCurr ) { bUseCol = false; break; } } if ( bUseCol ) sFieldNamesSeperatedByCommas+= (sColNameCurr + _T(",")); } if ( sFieldNamesSeperatedByCommas.at( int(sFieldNamesSeperatedByCommas.size()) - 1) == _T(',')) sFieldNamesSeperatedByCommas.erase( int(sFieldNamesSeperatedByCommas.size()) - 1 ); tstring sDML; sDML = StringOps::std_sprintf(_T("insert into TempTable1 SELECT %s FROM MyOriginalTable;\n") , sFieldNamesSeperatedByCommas.c_str() ); execDML( StringOps::tstringTo_stdString(sDML).c_str() ); sDML = StringOps::std_sprintf(_T("ALTER TABLE MyOriginalTable RENAME TO MyOriginalTable_old\n") ); execDML( StringOps::tstringTo_stdString(sDML).c_str() ); sDML = StringOps::std_sprintf(_T("ALTER TABLE TempTable1 RENAME TO MyOriginalTable\n") ); execDML( StringOps::tstringTo_stdString(sDML).c_str() ); sDML = ( _T("DROP TABLE MyOriginalTable_old;") ); execDML( StringOps::tstringTo_stdString(sDML).c_str() ); execDML( StringOps::tstringTo_stdString(_T("commit transaction;")).c_str() ); } 

如果有人需要(近乎)即时可用的PHP函数,下面是基于这个答案 :

 /** * Remove a column from a table. * * @param string $tableName The table to remove the column from. * @param string $columnName The column to remove from the table. */ public function DropTableColumn($tableName, $columnName) { // -- // Determine all columns except the one to remove. $columnNames = array(); $statement = $pdo->prepare("PRAGMA table_info($tableName);"); $statement->execute(array()); $rows = $statement->fetchAll(PDO::FETCH_OBJ); $hasColumn = false; foreach ($rows as $row) { if(strtolower($row->name) !== strtolower($columnName)) { array_push($columnNames, $row->name); } else { $hasColumn = true; } } // Column does not exist in table, no need to do anything. if ( !$hasColumn ) return; // -- // Actually execute the SQL. $columns = implode('`,`', $columnNames); $statement = $pdo->exec( "CREATE TABLE `t1_backup` AS SELECT `$columns` FROM `$tableName`; DROP TABLE `$tableName`; ALTER TABLE `t1_backup` RENAME TO `$tableName`;"); } 

与其他答案相反,在这种方法中使用的SQL似乎保留了列的数据types,而像接受的答案似乎导致所有列都是TEXTtypes。

更新1:

所使用的SQL具有保留autoincrement列的缺点。