0

現在、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;
4

5 に答える 5

0

このクエリを試してください..

    INSERT into constituent (constituent_name, constituent_ticker, sector_id) 
    values ("All Asia Asset Cp","AAA",
    (select id from sector where sector_name = "General Financial Sector"))

SQLフィドル

于 2013-09-14T10:31:17.057 に答える
0

セクター.id の代わりに、id だけで試してみてください。それは可能性が?

INSERT into constituent (constituent_name, constituent_ticker, sector_id) values ("All Asia Asset Cp","AAA",(select id from sector where sector_name = "General Financial Sector"))
于 2013-09-14T09:52:52.907 に答える
0

行が次のようになるように、列名を一重引用符で囲むことにより、不明な列の問題が修正されたようです。

INSERT into constituent (constituent_name, constituent_ticker, sector_id) values ("All Asia Asset Cp","AAA", (select 'id' from sector where sector_name = "General Financial Sector"))
于 2013-09-15T06:16:37.140 に答える
0

ごめんなさい。

不明な列は、アプリケーションが列を持たない正しい DB を指していたためで、PHPMyAdmin を介して、列を持った正しくない DB に手動で挿入しようとしていました。

私はアホです。

でもありがとう。

于 2013-09-15T14:54:41.757 に答える
0

あなたの使い方は挿入する方法ではありませ

//Initialize insertValues
insertValues = connect.prepareStatement(sql);

//Attempt to add the values into the database
try {
    insertValues.executeUpdate();
} catch (Exception e) {

    Log.e(CLASS_NAME, "createConstituent ", e);

}
于 2013-09-14T10:34:53.960 に答える