我正在尝试显示从另一个表中提取的外键值。 我使用的代码:
第一张表的代码:
CREATE TABLE trip (
tripID INT(10) PRIMARY KEY,
startTime TIME,
endTime TIME,
tripDate DATE,
price VARCHAR(5),
databaseID INT(10),
FOREIGN KEY (databaseID)
REFERENCES databasestorage(databaseID)
);
第二个表的代码:
CREATE TABLE databasestorage (
databaseID INT(10) PRIMARY KEY,
location VARCHAR(40)
);
将值插入两个表后,在trip
表中,databaseID
仍显示为null
,而其他所有列都正确。如何使databaseID
表格中的trip
显示databaseID
表中databasestorage
的值?
答案 0 :(得分:1)
您需要传递trip
表的主键,同时将数据插入INSERT INTO databasestorage(databaseID, location) VALUES('1021', 'Caulfield')
表吗?
databaseID
对于旅行表,您需要明确传递旅行表中 ---------v
INSERT INTO trip(tripID, startTime, endTime, tripDate, price,databaseID)
VALUES('101', '20:20:00', '20:40:00', '2016-10-22', '$5.25','1021')
-----^
列的值,外键不会自动插入
import UIKit
public struct Pixel {
public var value: UInt32
public var red: UInt8 {
get {
return UInt8(value & 0xFF)
}
set {
value = UInt32(newValue) | (value & 0xFFFFFF00)
}
}
public var green: UInt8 {
get {
return UInt8((value >> 8) & 0xFF)
}
set {
value = (UInt32(newValue) << 8) | (value & 0xFFFF00FF)
}
}
public var blue: UInt8 {
get {
return UInt8((value >> 16) & 0xFF)
}
set {
value = (UInt32(newValue) << 16) | (value & 0xFF00FFFF)
}
}
public var alpha: UInt8 {
get {
return UInt8((value >> 24) & 0xFF)
}
set {
value = (UInt32(newValue) << 24) | (value & 0x00FFFFFF)
}
}
public struct RGBAImage {
public var pixels: [Pixel]
public var width: Int
public var height: Int
public init?(image: UIImage) {
guard let cgImage = image.cgImage else { return nil }
// Redraw image for correct pixel format
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
width = Int(image.size.width)
height = Int(image.size.height)
let bytesPerRow = width * 4
let imageData = UnsafeMutablePointer<Pixel>.allocate(capacity: width * height)
guard let imageContext = CGContext(data: imageData, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo) else { return nil }
CGContextDrawImage(imageContext, CGRect(origin: CGPointZero, size: image.size), cgImage)
let bufferPointer = UnsafeMutableBufferPointer<Pixel>(start: imageData, count: width * height)
pixels = Array(bufferPointer)
imageData.deinitialize()
imageData.deallocate(capacity: width * height)
}
public func toUIImage() -> UIImage? {
let colorSpace = CGColorSpaceCreateDeviceRGB()
var bitmapInfo: UInt32 = CGBitmapInfo.byteOrder32Big.rawValue
bitmapInfo |= CGImageAlphaInfo.premultipliedLast.rawValue & CGBitmapInfo.alphaInfoMask.rawValue
let bytesPerRow = width * 4
let imageDataReference = UnsafeMutablePointer<Pixel>(mutating: pixels)
defer {
imageDataReference.deinitialize()
}
let imageContext = CGContext(data: imageDataReference, width: width, height: height, bitsPerComponent: 8, bytesPerRow: bytesPerRow, space: colorSpace, bitmapInfo: bitmapInfo, releaseCallback: nil, releaseInfo: nil)
guard let cgImage = imageContext!.makeImage() else {return nil}
let image = UIImage(cgImage: cgImage)
return image
}
答案 1 :(得分:0)
试试这个;)
将auto_increment
添加到表定义:
首先:
CREATE TABLE databasestorage (
databaseID INT(10) PRIMARY KEY auto_increment,
location VARCHAR(40)
);
第二名:
CREATE TABLE trip (
tripID INT(10) PRIMARY KEY auto_increment,
startTime TIME,
endTime TIME,
tripDate DATE,
price VARCHAR(5),
databaseID INT(10),
FOREIGN KEY (databaseID)
REFERENCES databasestorage(databaseID)
);
ADDITION (插入查询)
INSERT INTO trip(tripID, startTime, endTime, tripDate, price) VALUES('101', '20:20:00', '20:40:00', '2016-10-22', '$5.25');
在此查询中,无需传递tripID
auto_increment
字段的值,我认为您将databaseID
值传递给tripID
。因此,databasestorage.databaseID
插入后您获得的值应该在trip.databaseID
所以最后的查询是:
INSERT INTO trip(startTime, endTime, tripDate, price, databaseID) VALUES('20:20:00', '20:40:00', '2016-10-22', '$5.25', 101);
由于trip.databaseID
为INT
字段,因此无需添加引号(''
);