現在、2 つの文字列と外部キーを挿入するために使用したい以下のクエリがあります。ただし、外部キーの文字列を渡しているので、挿入時にキーを取得したいので、レコードは実際には次のように読み取られます。
"全アジア資産 Cp", "AAA", 1
以下は私が使用する文字列です:
String sql = "INSERT into constituent " +
"(constituent_name, constituent_ticker, sector_id) values (\""+
constituent.getConstituentName() + "\",\"" +
constituent.getConstituentTicker() + "\"," +
"(select id from sector where sector_name = \"" + constituent.getConstituentSector() + "\"))";
以下はクエリです。
INSERT into constituent (constituent_name, constituent_ticker, sector_id)
values ("All Asia Asset Cp","AAA",
(select sector.id from sector where sector_name = "General Financial Sector"))
ただし、以下のエラーが発生し、困惑しています。何か案は?
Unknown column 'sector.id' in 'field list'
phpMyAdmin でそのままクエリを実行すると機能します。
以下を使用して Java から実行すると、エラーがスローされます。
//Initialize insertValues
insertValues = connect.prepareStatement(sql);
//Attempt to add the values into the database
try {
insertValues.execute();
} catch (Exception e) {
Log.e(CLASS_NAME, "createConstituent ", e);
}
DDL セクター テーブルの場合:
CREATE TABLE `sector` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`sector_name` varchar(100) DEFAULT NULL,
`sector_url` varchar(500) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=39 DEFAULT CHARSET=latin1;
DDL 構成表の場合:
CREATE TABLE `constituent` (
`constituent_id` int(11) NOT NULL AUTO_INCREMENT,
`constituent_name` varchar(100) DEFAULT '',
`constituent_ticker` varchar(10) NOT NULL,
`constituent_isin_number` varchar(50) DEFAULT '',
`constituent_currency` varchar(10) DEFAULT '',
`sector_id` int(11) NOT NULL,
PRIMARY KEY (`constituent_id`),
KEY `sector_id` (`sector_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;