Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

select().bySQL 对于字段名为value的查询出错 #39

Closed
cotide opened this issue Nov 20, 2018 · 5 comments
Closed

select().bySQL 对于字段名为value的查询出错 #39

cotide opened this issue Nov 20, 2018 · 5 comments
Assignees
Labels
good first issue Good for newcomers question Further information is requested

Comments

@cotide
Copy link

cotide commented Nov 20, 2018

表设计 (MYSQL)

CREATE TABLE user_info (
  `user_id` INT (11) NOT NULL AUTO_INCREMENT COMMENT '用户Id', 
  `user_name` VARCHAR (255) DEFAULT NULL COMMENT '用户名', 
  `value`  VARCHAR (255)  NULL COMMENT '值',  
  PRIMARY KEY (user_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息';

数据 (MYSQL)

user_id user_name value
1 toby hello world

使用bySQL查询数据单列

正常情况

 @Test
public void query(){
        Anima.open(url,user,pass);
        String value =  select().bySQL(
                String.class,"select user_name from user_info where user_id = 1").one();
        System.out.println(value);
}

输出: toby

查询value

@Test
public void query(){
        Anima.open(url,user,pass);
        String value =  select().bySQL(
                String.class,"select `value` from user_info where user_id = 1").one();
        System.out.println(value);
}

异常信息

15:31:07.962 [main] DEBUG org.sql2o.Query - Execute SQL => select `value` from user_info where user_id = 8
15:31:07.966 [main] DEBUG org.sql2o.Query - Parameters  => []

org.sql2o.Sql2oException: Database error: Unknown column 'value' in 'field list'

	at org.sql2o.Query$ResultSetIterableBase.<init>(Query.java:216)
	at org.sql2o.Query$1.<init>(Query.java:292)
	at org.sql2o.Query.executeAndFetchLazy(Query.java:292)
	at org.sql2o.Query.executeAndFetchFirst(Query.java:343)
	at org.sql2o.Query.executeAndFetchFirst(Query.java:335)
	at io.github.biezhi.anima.core.AnimaQuery.queryOne(AnimaQuery.java:1057)
	at io.github.biezhi.anima.core.ResultList.one(ResultList.java:46)
	at io.github.biezhi.anima.SelectOneTest.query(SelectOneTest.java:20)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'value' in 'field list'
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
	at com.mysql.jdbc.Util.getInstance(Util.java:408)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:943)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3909)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2527)
	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2680)
	at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2501)
	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
	at org.sql2o.Query$ResultSetIterableBase.<init>(Query.java:213)
	... 29 more


Process finished with exit code 255
@hellokaton hellokaton self-assigned this Nov 29, 2018
@hellokaton hellokaton added good first issue Good for newcomers question Further information is requested labels Nov 29, 2018
@hellokaton
Copy link
Owner

hellokaton commented Nov 29, 2018

@cotide 你的实体中是否有定义 value 这个字段?

@cotide
Copy link
Author

cotide commented Dec 3, 2018

我使用实体进行查询能正常工作

实体

@Table(name = "user_info")
@Data
 public class UserInfo extends Model{

 @Column(name = "user_id")
 private int userId;

 @Column(name = "`value`")
  private String value;
}

测试代码

@Test
public void query(){ 
        Anima.open(url,user,pass); 
        UserInfo result   = select().from(UserInfo.class).where(UserInfo::getUserId,1).one();
        System.out.println(result.getUserId());
        System.out.println(result.getValue());
}

输出结果

09:48:40.244 [main] DEBUG org.sql2o.Query - Execute SQL => SELECT * FROM user_info WHERE user_id = ? LIMIT 1
09:48:40.247 [main] DEBUG org.sql2o.Query - Parameters  => [1]
09:48:40.273 [main] DEBUG org.sql2o.Query - Total       => 29 ms, execution: 18 ms, reading and parsing: 11 ms; executed [null]
1
hello world

能正确输出~

使用bySQL查询user_id字段值

@Test
public void query(){ 
        Anima.open(url,user,pass);
        int value =  select().bySQL(
                Integer.class,"select `user_id` from user_info where user_id = 1").one();
        System.out.println(value);
}

能正确输出内容

image

使用bySQL查询value字段值

 @Test
 public void query(){ 
        Anima.open(url,user,pass);
       String value =  select().bySQL(
                String.class,"select `value` from user_info where user_id = 1").one();
        System.out.println(value);
}

输出内容为空~
image

@cotide
Copy link
Author

cotide commented Dec 3, 2018

使用bySQL 查询返回对象为String.class并且针对value字段时候不能正常工作

@hellokaton
Copy link
Owner

hellokaton commented Dec 3, 2018

这个问题已经修复了,在 6bedcc0 ,你可以继续使用快照版本尝试。

mvn clean compile -U

记得更新一下最新依赖

@cotide
Copy link
Author

cotide commented Dec 5, 2018

OK~ 获取最新代码,问题已经解决了~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants