{"id":212,"date":"2024-01-29T14:23:53","date_gmt":"2024-01-29T22:23:53","guid":{"rendered":"https:\/\/blog.sacko.dev\/?p=212"},"modified":"2024-01-29T14:26:46","modified_gmt":"2024-01-29T22:26:46","slug":"how-to-edit-a-sqlite-database-in-the-terminal","status":"publish","type":"post","link":"https:\/\/blog.sacko.dev\/?p=212","title":{"rendered":"How to Edit a SQLite Database in the Terminal"},"content":{"rendered":"\n<p>I usually would use an editor for SQLite called DB Browser (<a href=\"https:\/\/sqlitebrowser.org\/\">https:\/\/sqlitebrowser.org\/<\/a>), but sometimes if I am accessing a server with SSH, I do not want to deal with having to use a GUI program to edit my database.<\/p>\n\n\n\n<p>To be able to modify your database, you may need to alter permissions on the database file, as well as the permissions on the folder it is in, or you will get an error like <strong>Runtime error: attempt to write a readonly database (8)<\/strong>, meaning you would not have the proper permissions to edit the database<strong>.<\/strong> You will want to at least have read and write permissions for both of them.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>chown <strong>user:user<\/strong> database_name.db\nchown <strong>user:user<\/strong> \/path\/name\nchmod <strong>660<\/strong> database_name.db\nchmod <strong>660<\/strong> \/path\/name<\/code><\/pre>\n\n\n\n<p>Once this is done, install sqlite3 and start it up, then open your database file within sqlite3.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>apt install sqlite3\nsqlite3\n.open <strong>database_name.db<\/strong>;<\/code><\/pre>\n\n\n\n<p>To test if everything is working, use the .tables command to list the names of all the tables in the database. If it lists any tables, you can view a table&#8217;s column info with PRAGMA table_info(). You can also view all the data in a table with a SELECT query.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># List all table names\n.tables\n# List column info for a table\nPRAGMA table_info(<strong>table_name<\/strong>);\n# Show all rows in a table\nSELECT * FROM <strong>table_name<\/strong>;\n# Show 10 rows from a table\nSELECT * FROM <strong>table_name<\/strong> LIMIT 10;\n# Show a row in a table with the matching ID\nSELECT * FROM <strong>table_name<\/strong> WHERE <strong>id_name<\/strong> = <strong>value<\/strong>;<\/code><\/pre>\n\n\n\n<p>If you have a row you would like to change, you will need to know the name of the ID column, the ID value for that row, and the name of the column you want to change for that row. For example, to change the value of a String value in a row:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>UPDATE <strong>table_name<\/strong> SET <strong>column_name<\/strong> = \"Hello, world!\" WHERE <strong>id_name<\/strong> = <strong>value<\/strong>;<\/code><\/pre>\n\n\n\n<p>If the user you are logged in as has the proper read\/write permissions, this should work without a problem. To check if the change was successful, run a SELECT query for that row.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM <strong>table_name<\/strong> WHERE <strong>id_name<\/strong> = <strong>value<\/strong>;<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I usually would use an editor for SQLite called DB Browser (https:\/\/sqlitebrowser.org\/), but sometimes if I am accessing a server with SSH, I do not want to deal with having to use a GUI program to edit my database. To be able to modify your database, you may need to alter permissions on the database <a href=\"https:\/\/blog.sacko.dev\/?p=212\" class=\"more-link\">&#8230;<span class=\"screen-reader-text\">  How to Edit a SQLite Database in the Terminal<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/212"}],"collection":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=212"}],"version-history":[{"count":2,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/212\/revisions"}],"predecessor-version":[{"id":216,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=\/wp\/v2\/posts\/212\/revisions\/216"}],"wp:attachment":[{"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sacko.dev\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}