SQLite dump: dump specific table into file, dump only table structure

sqlite3 shell का .dump command एक table को उन SQL statements में बदल देता है जो इसे rebuild करेंगे, और .schema बिना data के सिर्फ़ structure dump करता है।

8 min read · 10 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Exam cell को paper पर proof चाहिए

Exam cell अब ResultDesk पर भरोसा करता है, एक condition के साथ: "marks table को छूने से पहले, हमें एक copy दीजिए जो हम पढ़ और restore कर सकें।"

college.db की एक copy उनके लिए binary soup है। उन्हें table readable SQL के रूप में चाहिए: exact CREATE TABLE और INSERT statements जो इसे शून्य से rebuild करेंगे।

sqlite3 command shell यह एक dot-command से करता है: .dump। इसे सीखिए और कोई भी table एक text file बन जाती है जिसे आप पढ़, mail, या replay कर सकते हैं।

Theory

Recipe, पका हुआ dish नहीं

college.db copy करना एक पके thali की photo लेने जैसा है: exact, पर आप नहीं देख सकते इसमें क्या गया।

.dump recipe लिख देता है: हर step (CREATE TABLE) और हर ingredient (हर INSERT), plain text में। Recipe वाला कोई भी बाद में वही dish बना सकता है, किसी भी stove पर (किसी भी SQLite version पर), और cooking से पहले एक line पढ़ या edit भी कर सकता है। जो backups आप पढ़ सकें वही backups हैं जिन पर भरोसा कर सकते हैं।

Theory

Dot-commands: shell की अपनी language

sqlite3 shell में (terminal से sqlite3 college.db), दो languages साथ रहती हैं:

  • SQL एक semicolon से ख़त्म होती है और database से बात करती है: SELECT * FROM marks;
  • Dot-commands एक dot से शुरू होते हैं, कोई semicolon नहीं लेते, और shell को ख़ुद command करते हैं: .dump, .schema, .output, .read।

Dot-commands SQL NAHI हैं: वे Python या किसी भी program के अंदर नहीं चलेंगे, सिर्फ़ shell में। Exams बिल्कुल यही distinction check करते हैं।

Follow along

एक table को एक file में dump कीजिए

  1. Database पर shell खोलिए Terminal: sqlite3 college.db (prompt sqlite> में बदलता है)
  2. Output को एक file पर point कीजिए: .output marks_backup.sql अब से, shell जो भी print करता है वह screen के बजाय marks_backup.sql में जाता है।
  3. Table dump कीजिए: .dump marks CREATE TABLE marks statement के बाद प्रति row एक INSERT लिखता है।
  4. Output वापस screen पर point कीजिए: .output stdout इसे भूलिए और बाद का हर query result चुपचाप file में ग़ायब हो जाता है।
  5. Verify कीजिए: marks_backup.sql को किसी भी text editor में खोलिए आपको पढ़ना चाहिए CREATE TABLE marks(...); INSERT INTO marks VALUES(101,'DBMS',78); और आगे।

Theory

सिर्फ़ Structure: .schema

कभी-कभी आपको बिना data के skeleton चाहिए: design एक teammate के साथ share करना, या एक नए machine पर empty tables recreate करना।

  • .schema marks सिर्फ़ CREATE TABLE marks (...) statement print करता है।
  • अकेला .schema हर table, index और trigger का structure print करता है।

Rule of thumb: .dump = structure + data, .schema = सिर्फ़ structure। दोनों text हैं; दोनों को बाद में .read से replay किया जा सकता है।

Quiz

.dump marks से produce हुई file में EXACTLY क्या होता है?

  1. SQL text: CREATE TABLE statement plus प्रति row एक INSERT statement
  2. marks table की एक compressed binary copy
  3. एक header row वाली CSV file
  4. सिर्फ़ data values, बिना किसी structure के
Show the answer

SQL text: CREATE TABLE statement plus प्रति row एक INSERT statement

एक dump एक logical backup है: executable SQL जो table को शून्य से rebuild करता है: पहले CREATE TABLE, फिर हर row एक INSERT के रूप में। यह binary नहीं है (वह .db file copy करना होगा) और CSV नहीं है (वह .mode csv workflow है, दो lessons आगे)। क्योंकि structure data के SAATH travel करता है, file को एक empty database पर replay करना बस काम करता है।

Think first

Stuck shell पहचानिए

एक student .output backup.sql फिर .dump marks चलाता है, फिर SELECT * FROM students; type करता है और screen पर कुछ NAHI देखता है। Database ठीक है। tap करने से पहले: क्या हुआ, और कौन सा एक command इसे fix करता है?

Show the answer

Shell अभी भी सब कुछ backup.sql में redirect कर रहा है: SELECT काम किया, पर इसकी rows file में गईं, dump के बाद appended। Fix है .output stdout, जो printing को वापस screen पर point करता है। यह classic .output trap है: redirection तब तक on रहता है जब तक आप इसे off न करें, और shell कोई reminder नहीं देता।

Watch out

Marks कहाँ लीक होते हैं

Dot-commands पर Semicolons: .dump; shell को confuse करता है। Dot-commands कोई semicolon नहीं लेते; SQL लेता है।

Shell के बाहर dot-commands चलाना: वे shell features हैं, SQL नहीं: Python में cursor.execute('.dump') fail होता है।

Dump पूछे जाने पर "file copy कीजिए" जवाब देना: एक file copy एक valid binary backup है, पर सवाल SQL-text mechanism माँगता है; .dump नाम दीजिए और इसका output क्या रखता है।

Theory

Professionals text dumps क्यों पसंद करते हैं

Text dumps git में साफ़ diff करते हैं (कल का dump बनाम आज का बिल्कुल दिखाता है कौन सी rows बदलीं), SQLite version upgrades झेलते हैं, और restore से पहले edit किए जा सकते हैं (text file में एक bad row fix कीजिए)। अगला lesson इसे एक table से पूरे database तक scale करता है, और वह restore command जोड़ता है जो आपकी recipe को वापस dish में बदल देता है।

Summary

Key takeaways

  • Dot-commands (.dump, .schema, .output) sqlite3 SHELL commands हैं: पहले dot, कोई semicolon नहीं, SQL नहीं।
  • .dump marks वह SQL लिखता है जो table rebuild करता है: CREATE TABLE + प्रति row एक INSERT।
  • .schema marks सिर्फ़ structure देता है; अकेला .schema हर object cover करता है।
  • Dump करने से पहले .output file.sql से redirect कीजिए; हमेशा .output stdout से वापस लौटिए।
  • एक dump एक logical, readable, portable backup है; .db file copy करना एक binary snapshot है।
  • Memory hook: recipe, पका हुआ dish नहीं।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Database backup and CSV handling

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati