本文介绍了如何加密sqlite数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我用c#编写代码。 我有一个加密的sqlite数据库。我在SQLite Maestro中创建了这个数据库。在这个程序上,右键单击,管理加密,我给我想要的密码。此文件只有数据库表,因此没有数据。我在我的代码中将此文件添加为Embedded Resource。但是,我尝试读取此数据库文件并出错。 错误:文件不是数据库 b $ b 我使用实体框架。 我有什么试过: //使用新名称创建加密的sqlite数据库文件 internal static void CreateDatabase(string fullName) { CodeFirst = true; DataSource = fullName; Assembly asm = Assembly.GetExecutingAssembly(); string ns = typeof(Context).Namespace; if(File.Exists(fullName)) { File.Delete(fullName); //备份文件将删除 } 使用(Stream stream = asm.GetManifestResourceStream(${ ns} .sqlitedatabasefile)) { using(FileStream fileStream = new Fil eStream(fullName,FileMode.CreateNew,FileAccess.Write)) { stream.CopyTo(fileStream); } } } //然后,我写下以下代码来控制版本和连接数据库 public Context():base(new SQLiteConnection { ConnectionString = new SQLiteConnectionStringBuilder { DataSource = DataSource, ForeignKeys = true, BinaryGUID = false, Pooling = false, SetDefaults = false, PageSize = 1024, Version = 3, //密码=1234//无法正常工作 } .ConnectionString },true) {b DbConfiguration.SetConfiguration(新的SQLiteConfiguration()); if(Database.Connection是SQLiteConnection) conn) { //conn.setPassword(\"123); //不工作 conn.Open(); using(SQLiteCommand cmd = conn.CreateCommand()) { bool hasColumn = false; cmd.CommandText =PRAGMA table_info(Student); //学生是桌子。 使用(SQLiteDataReader reader = cmd.ExecuteReader())//错误。 { int nameIndex = reader.GetOrdinal(Name); while(reader.Read()) { if(reader.GetString(nameIndex).Equals(Age)) { hasColumn = true; break; } } } //在学生中添加年龄栏table if(!hasColumn) { Database.ExecuteSqlCommand(ALTER TABLE Student ADD COLUMN年龄整数DEFAULT -1); //Database.ExecuteSqlCommand($@\"UPDATE SET SET Version = 2,ReleaseDate ='{DateTime.Now.ToShortDateString()}'); var version = Setting.FirstOrDefaultAsync(); version.Result.Version = 2; version.Result.ReleaseDate = DateTime.Now; SaveChanges(); System.Windows.Forms.MessageBox.Show(您的数据库已更新...,信息消息,System.Windows.Forms.MessageBoxButtons.OK,System.Windows.Forms.MessageBoxIcon.Information); } } } }I write code with c#.I have a encrypted sqlite database. I created this database in SQLite Maestro. on this program, right click, manage encryption, I give password which I want to. This file has only database tables, so it has no data. I added this file in my code as Embedded Resource. But, I try reading this database file and take error.Error : file is not databaseI use Entity Framework.What I have tried://create encrypted sqlite database file with new nameinternal static void CreateDatabase(string fullName) { CodeFirst = true; DataSource = fullName; Assembly asm = Assembly.GetExecutingAssembly(); string ns = typeof(Context).Namespace; if (File.Exists(fullName)) { File.Delete(fullName);//Backup file will delete } using (Stream stream = asm.GetManifestResourceStream($"{ns}.sqlitedatabasefile")) { using (FileStream fileStream = new FileStream(fullName, FileMode.CreateNew, FileAccess.Write)) { stream.CopyTo(fileStream); } } }//Then, I write below codes to control of version and to connnect database public Context() : base(new SQLiteConnection { ConnectionString = new SQLiteConnectionStringBuilder { DataSource = DataSource, ForeignKeys = true, BinaryGUID = false, Pooling = false, SetDefaults = false, PageSize = 1024, Version = 3, //Password= "1234" //not working }.ConnectionString }, true) { DbConfiguration.SetConfiguration(new SQLiteConfiguration()); if (Database.Connection is SQLiteConnection conn) { //conn.setPassword("123"); //not working conn.Open(); using (SQLiteCommand cmd = conn.CreateCommand()) { bool hasColumn= false; cmd.CommandText = "PRAGMA table_info(Student)"; //Student is table. using (SQLiteDataReader reader = cmd.ExecuteReader())//ERROR. { int nameIndex = reader.GetOrdinal("Name"); while (reader.Read()) { if (reader.GetString(nameIndex).Equals("Age")) { hasColumn= true; break; } } } //Add Age column in Student table if (!hasColumn) { Database.ExecuteSqlCommand("ALTER TABLE Student ADD COLUMN Age integer DEFAULT -1"); //Database.ExecuteSqlCommand($@"UPDATE Setting SET Version= 2 , ReleaseDate ='{DateTime.Now.ToShortDateString()}'"); var version = Setting.FirstOrDefaultAsync(); version.Result.Version = 2; version.Result.ReleaseDate = DateTime.Now; SaveChanges(); System.Windows.Forms.MessageBox.Show("Your database updated...", "Information Message", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); }}}}推荐答案{ns} .sqlitedatabasefile)) { using(FileStream fileStream = new FileStream(fullName,FileMode.CreateNew,FileAccess) 。写)) { stream.CopyTo(fileStre am); } } } //然后,我写下面的代码来控制版本和连接数据库 public Context():base(new SQLiteConnection { ConnectionString = new SQLiteConnectionStringBuilder { DataSource = DataSource, ForeignKeys = true, BinaryGUID = false, Pooling = false, SetDefaults = false, PageSize = 1024, Version = 3 , //密码=1234//无法使用 } .ConnectionString },true) { DbConfiguration.SetConfiguration(new SQLiteConfiguration()); if(Database.Connection是SQLiteConnection conn) { //conn.setPassword(\"123); //不工作 conn.Open(); using(SQLiteCommand cmd = conn.CreateCommand()) { bool hasColumn = false; cmd.CommandText =PRAGMA table_info(Student); //学生是桌子。 使用(SQLiteDataReader reader = cmd.ExecuteReader())//错误。 { int nameIndex = reader.GetOrdinal(Name); while(reader.Read()) { if(reader.GetString(nameIndex).Equals(Age)) { hasColumn = true; break; } } } //在学生中添加年龄栏table if(!hasColumn) { Database.ExecuteSqlCommand(ALTER TABLE Student ADD COLUMN年龄整数DEFAULT -1); //Database.ExecuteSqlCommand("{ns}.sqlitedatabasefile")) { using (FileStream fileStream = new FileStream(fullName, FileMode.CreateNew, FileAccess.Write)) { stream.CopyTo(fileStream); } } }//Then, I write below codes to control of version and to connnect database public Context() : base(new SQLiteConnection { ConnectionString = new SQLiteConnectionStringBuilder { DataSource = DataSource, ForeignKeys = true, BinaryGUID = false, Pooling = false, SetDefaults = false, PageSize = 1024, Version = 3, //Password= "1234" //not working }.ConnectionString }, true) { DbConfiguration.SetConfiguration(new SQLiteConfiguration()); if (Database.Connection is SQLiteConnection conn) { //conn.setPassword("123"); //not working conn.Open(); using (SQLiteCommand cmd = conn.CreateCommand()) { bool hasColumn= false; cmd.CommandText = "PRAGMA table_info(Student)"; //Student is table. using (SQLiteDataReader reader = cmd.ExecuteReader())//ERROR. { int nameIndex = reader.GetOrdinal("Name"); while (reader.Read()) { if (reader.GetString(nameIndex).Equals("Age")) { hasColumn= true; break; } } } //Add Age column in Student table if (!hasColumn) { Database.ExecuteSqlCommand("ALTER TABLE Student ADD COLUMN Age integer DEFAULT -1"); //Database.ExecuteSqlCommand( @UPDATE设置SET Version = 2,ReleaseDate ='{DateTime.Now.ToShortDateString()}'); var version = Setting.FirstOrDefaultAsync(); version.Result.Version = 2; version.Result.ReleaseDate = DateTime.Now; SaveChanges(); System.Windows.Forms.MessageBox.Show(您的数据库已更新...,信息消息,System.Windows.Forms.MessageBoxButtons.OK,System.Windows .Forms.MessageBoxIcon.Information); } } } }@"UPDATE Setting SET Version= 2 , ReleaseDate ='{DateTime.Now.ToShortDateString()}'"); var version = Setting.FirstOrDefaultAsync(); version.Result.Version = 2; version.Result.ReleaseDate = DateTime.Now; SaveChanges(); System.Windows.Forms.MessageBox.Show("Your database updated...", "Information Message", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information); }}}}也可以尝试 SQL Maestro技术支持 [ ^ ] 这篇关于如何加密sqlite数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-02 19:35