Background: The prevalence of diabetes mellitus was often reported as higher in mental illness inpatients compared to the general population, but large study of the prevalence of diabetes among mental illness inpatients in China was scarce. We aim to investigate the prevalence, trends, and major risk factors of diabetes mellitus among adult mental disorder inpatients in Beijing, China. Methods: We did a longitudinal observational study using data from Beijing Municipal Commission of Health and Family Planning Information Center, including 188856 adult inpatients with mental disorders in 19 specialized psychiatric hospital from 2005 to 2018 in Beijing. Data of demographic characteristics was obtained from electronic health record (EHR) data. Schizophrenia, diabetes, and comorbidities were defined according to ICD-10 codes of discharge diagnosis. Overall prevalence of diabetes in adult mental disorder inpatients was calculated, and annual prevalence of diabetes was calculated and adjusted to the overall participant population. Univariate and multivariate logistic regression analysis was performed to obtain crude odds ratios (cORs) and adjusted odds ratios (aORs) for the risk of diabetes in patients with different demographic characteristics, with schizophrenia versus other mental disorders, and with different comorbidities. We also calculated the prevalence of diabetes in different age groups under a stratification of schizophrenia or other mental disorders. Findings: Out of 188856 adult inpatients, 23558 had diabetes mellitus, the prevalence was 12.47% (95% confidence interval (CI): 12.33-12.62%). The prevalence was 13.42% (95%CI: 13.18-13.66%) among patients with schizophrenia, and 11.81% (95%CI: 11.61-12.00%) among patients with other mental disorders. 87.48% patients with diabetes mellitus was coded as type 2 diabetes mellitus (T2DM), the prevalence of T2DM was 10.91% (95%CI: 10.77-11.05%). During 2005-2018, the prevalence of diabetes mellitus in adult patients were increasing with admitted year, from adjusted prevalence of 6.05% in 2005, to 12.41% in 2010, 14.48% in 2015, 14.08% in 2018. After adjusting for gender, ethnic origin, marriage status, payment, and hospital grade, results from the multivariate analysis showed that increasing age, diagnosed as schizophrenia (aOR=1.22, 95%CI: 1.18-1.26), comorbidity with hypertensive diseases (aOR=3.11, 95%CI: 3.01-3.22), lipoprotein metabolism disorders (aOR=2.03, 95%CI: 1.96-2.10), fatty liver (aOR=1.91, 95%CI: 1.84-1.98) were major risk factors of diabetes mellitus in adult mental disorder inpatients. Interpretation: The prevalence of diabetes mellitus was high among adult mental disorder inpatients in Beijing, China. Patients who were elderly, with schizophrenia, and those with hypertensive diseases, lipoprotein metabolism disorders, and fatty liver had higher risk for diabetes mellitus. Prevention and treatment of diabetes mellitus is of utmost relevance in hospitalized mental illness patients. Funding Statement: This study was supported by the grant from National Natural Science Foundation of China (No. 71874003 and No. 81703240). Declaration of Interests: The authors declare no competing interests. Ethics Approval Statement: The study used information that is available in database of the Beijing Municipal Commission of Health and Family Planning Information Center, and all identifiable information was removed. Patients and the public were not involved in the design and conduct of the study.