将一个数据库文件的列替换为另一数据库文件的列

时间:2018-11-06 19:04:50

标签: database sqlite

我有一个包含表数据的db文件:

CREATE TABLE songs (
id integer primary key not null,
title text not null, --title of song
composer text, --composer(s) of song
bookcode text, --book song is from
page integer, --page in book song appears on
length integer --length of song in pages
);

但是所有的composer和length值都为空,我希望将它们替换为其他db文件中存储数据的composer和length值,例如:

  CREATE TABLE songs(
  title text NOT NULL, --title of the song
  composer text NOT NUll, --composer or composers of the song
  bookcode text NOT NULL,  --book code for the book the song is from
  page int, --page number in book where song appears
  length int, --number of pages the song occupies in the book
  studentnum text NOT NULL,  --student number of who contributed the data
  primary key (title,bookcode,page,studentnum)
  );

我试图弄清楚如何在mySQL中同时访问两者,但sqlite3一次只能在一个数据库上打开。

1 个答案:

答案 0 :(得分:0)

您可以ATTACH将一个数据库(默认情况下最多10个,SQLITE)复制到另一个数据库(主数据库/主数据库),然后使用架构 main 来访问两者/全部。 / primary / main数据库以及其他附加数据库的命名架构(按照AS关键字)。

例如:-

DETACH test002;
ATTACH 'D:\Databases\SQLite\Navicat Databases\test002.db' AS test002;
SELECT * FROM main.sqlite_master;
SELECT * FROM test002.sqlite_master;
PRAGMA database_list; -- List the attached databases

以上结果(仅出于概念证明):-

enter image description here

enter image description here

enter image description here

评论

  

终端说找不到命令ATTACH

很少是命令行用户,但以下显示 ATTACH 通过命令提示符使用:-

C:\Users\Mike>sqlite3
SQLite version 3.22.0 2018-01-22 18:45:57
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> ATTACH 'D:\Databases\SQLite\Navicat Databases\test002.db' AS test002;
sqlite> ATTACH 'D:\Databases\SQLite\Navicat Databases\gransha.db' AS test001;
sqlite> SELECT * FROM test002.sqlite_master;
table|phonebook|phonebook|2|CREATE TABLE phonebook(name TEXT PRIMARY KEY, phonenumber TEXT)
index|sqlite_autoindex_phonebook_1|phonebook|3|
sqlite> SELECT * FROM test001.sqlite_master;
table|_Questions_old_20180428|_Questions_old_20180428|2|CREATE TABLE "_Questions_old_20180428" (
  "_QuestionID" INTEGER NOT NULL,
  "QuestionResult" integer,
  "QuestionTypeID" INTEGER,
  "QuestionTopicID" INTEGER,
  "LogUserID" INTEGER,
  PRIMARY KEY ("_QuestionID")
) ............