The invention is suitable for the technical field of computers, and provides a code generation method and a code generation device. The code generation method comprises: displaying a data table corresponding to a data source on a first interface in response to the operation of selecting the data source on the first interface, and obtaining table field information of the selection operation is obtained in response to the selection operation on the data table; and reading the corresponding code template from the first storage position according to the table field information, updating the target variable in the code template according to the table field information, and finally converting the code template after the target variable is updated to obtain a target code. According to the embodiment of the invention, various code templates with variables can be provided, the code template can be determined according to the selection result after the user selects the corresponding data source and the table field information, and the target code can be obtained after the variables in the code template are updated into the table field information, so that the development time is greatly saved, the code style is unified, and later maintenance is easy.