我想通过将表从_bak.db复制到正在使用的.db来恢复数据库的备份。
conn = sqlite3.connect(os.path.join("data", "db", "Kanji-story.db"))
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS current")
c.execute("ATTACH DATABASE ? AS db2", (os.path.join('data', 'db', 'Kanji-story_bak.db'),))
# TODO Code for Creating table with the same structure
c.execute("INSERT INTO main.current SELECT * FROM db2.current")
要执行最后一个语句,我首先要在Kanji-story.db中创建一个与Kanji-story_bak.db结构相同的表(参见#TODO)。如何使用相同结构 创建 TABLE ?我知道SQLite3中有 .schema 命令,但是如何有效地使用该命令创建一个新表呢?
答案 0 :(得分:3)
受到@CL答案的启发。 ,完整的代码是:
conn = sqlite3.connect(os.path.join("data", "db", "Kanji-story.db"))
c = conn.cursor()
c.execute("DROP TABLE IF EXISTS current")
c.execute("ATTACH DATABASE ? AS db2", (os.path.join('data', 'db', 'Kanji-story_bak.db'),))
c.execute("SELECT sql FROM db2.sqlite_master WHERE type='table' AND name='current'")
c.execute(c.fetchone()[0]) # Contains: CREATE TABLE current (framenum INTEGER, nextKanji INTEGER)
c.execute("INSERT INTO main.current SELECT * FROM db2.current")
conn.commit()
conn.close()
答案 1 :(得分:2)
SQLite没有执行间接命令的机制。
要获取原始表定义,您已运行function drawChart (data) {
// Setup Crossfilter dimensions and groups
var nation = crossfilter(data),
byScr = nation.dimension(function(d){ return d.score; }),
byScrGrp = byScr.group().reduceCount(),
byHosp = nation.dimension(function(d){ return d.FIPS; });
// Histogram X-Axis ordinal scale
var x = d3.scale.ordinal()
.domain(d3.range(0,2,0.1))
.rangeBands([0, width, 0.5, 0.5 ]);
var brushScale = d3.scale.linear()
.domain([0,2])
.range([0, width]);
// Histogram Y-Axis linear scale
var y = d3.scale.linear()
.domain([0, d3.max(byScrGrp.all(), function(d){ return d.value; })])
.range([height/2, 0]);
// SVG Brush control object
var brush = d3.svg.brush()
.x(brushScale)
.on("brushstart", brushstart)
.on("brush", brushmove)
.on("brushend", brushend)
// Histogram SVG containiner
var svg = d3.select("#histogram").append("svg:svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height/2 + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Histogram bars
svg.selectAll("rect")
.data(byScrGrp.all())
.enter().append("rect")
.attr("x", function(d,i){ return i * (width / byScrGrp.size() ); })
.attr("width", width / byScrGrp.size() - barPadding )
.attr("y", function(d){ return y(d.value) ; })
.attr("height", function(d){ return ((height/2) -y(d.value)); })
.attr("fill", function(d){ return color(d.key); }) .on("mouseover", function(d){ d3.select(this).attr("fill", "teal"); console.log(d.key); })
.on("mouseout", function(d) { d3.select(this).attr("fill", function(d){return color(d.key);}) } );
// Histogram X Axis Object
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(10);
// X Axis
svg.append("g")
.attr("class", "axis")
.call(xAxis)
.attr("transform", "translate(0, " + height/2 + ")") ;
// X Axis Label
svg.append("text")
.attr("transform", "translate(" + (width/2) + " ," + (height/2 + margin.bottom) + ")")
.attr("text-anchor", "middle")
.text("MSPB Score");
// Histogram Y Axis Object
var yAxis = d3.svg.axis().scale(y).orient("left").ticks(10);
// Add Y Axis
svg.append("g")
.attr("class", "axis")
.call(yAxis)
.attr("transform", "translate(" + margin.left + ",0)");
// Add Y Axis Label
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - 10)
.attr("x", 0 - (height/4))
.attr("text-anchor", "middle")
.text("Number of Hospitals");
var brushg = svg.append("g")
.attr("class", "brush")
.call(brush)
brushg.select('.background')
.attr('width', width);
brushg.selectAll("rect")
.attr("height", height/2);
function brushstart() {
svg.classed("selecting", true);
}
function brushmove() {
var s = brush.extent();
brushg.classed("selected", function(d) { return s[0] <= (d = x(d)) && d <= s[1]; });
}
function brushend() {
if (!d3.event.sourceEvent) return;
d3.select(this).transition()
.call(brush.extent(brush.extent().map(function(d){return d3.round(d,2); })));
var extent = brush.extent(); console.log(extent);
}
}
内部使用的相同查询:
.schema
答案 2 :(得分:0)
我的sqlite3
版3.18.0
有create table main.current as select * from db2.current