我试图将几何体(POINT,LINE,POLYGON)插入到sqlite DB(spatialite)中的表中,所有代码都运行正常但是在查看行时,几何体的列始终是NULL。
INSERT后DB中的信息
应该如何:
字符串SQL的结果没问题,我正在使用
INSERT INTO Table1( Codigo, Nombre, Geometry) VALUES('1','Andalucia',GeomFromText('POLYGON((-75.8396284742612 8.39962954808519, ..., -75.8396284742612 8.39962954808519))', 4326));
INSERT INTO Table2( Codigo, Nombre, Geometry) VALUES('1','Andalucia',GeomFromText('POINT(-75.8361480385065 8.40883548152381)', 4326));
这是我在C#上的代码:
public SQLiteConnection create_Open_SpatilaDB(string dbName)
{
try
{
dbName += DateTime.Now.ToString("ddMMyyyyHHmmssff") + ".sqlite";
string ruta = HttpContext.Current.Server.MapPath("/Uploads/" + dbName);
string strConexion = "Data Source=" + ruta + ";Version=3;";
SQLiteConnection conexion = null;
if (!File.Exists(ruta))
{
// create the connection
conexion = new SQLiteConnection(strConexion);
//Abrir la conexión
conexion.Open();
//Load the lib
conexion.EnableExtensions(true);
conexion.LoadExtension("libspatialite-2.dll");
//Create tables
string SQL = "CREATE TABLE AppVersion ("
+ " versionCode INTEGER NOT NULL PRIMARY KEY,"
+ " state INTEGER NOT NULL);";
SQLiteCommand cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
SQL = "CREATE TABLE Table1 ("
+ " Codigo TEXT NOT NULL PRIMARY KEY,"
+ " Nombre TEXT,"
+ " Geometry POLYGON);";
cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
SQL = "CREATE TABLE Table2("
+ " Codigo TEXT NOT NULL PRIMARY KEY,"
+ " Nombre TEXT NOT NULL,"
+ " Geometry POIN);";
cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
.
.
.
}
else
{
conexion = new SQLiteConnection(strConexion);
conexion.Open();
conexion.EnableExtensions(true);
conexion.LoadExtension("libspatialite-2.dll");
}
return conexion;
}
catch (Exception)
{
throw;
}
}
在这里我插入信息:
public void insertInfo(SQLiteConnection conexion)
{
try
{
string SQL = "BEGIN";
SQLiteCommand cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
//Insert info table1
List<Table1> list = getInfoTable1();
foreach (Table1 item in list)
{
SQL = "INSERT INTO Table1("
+ " Codigo,"
+ " Nombre,"
+ " Geometry) VALUES("
+ "'" + item.Codigo + "',"
+ "'" + item.Nombre + "',"
+ "GeomFromText(" + item.geom + "));";
cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
}
//Insert info table1
List<Table2> list = getInfoTable2();
foreach (Table2 item in list)
{
SQL = "INSERT INTO Table2("
+ " Codigo,"
+ " Nombre,"
+ " Geometry) VALUES("
+ "'" + item.Codigo + "',"
+ "'" + item.Nombre + "',"
+ "GeomFromText(" + item.geom + "));";
cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
}
.
.
.
SQL = "COMMIT";
cmd = new SQLiteCommand(SQL, conexion);
cmd.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
}
答案 0 :(得分:1)
我认为您必须将表格的列标记为几何列;否则它不会被识别为几何列。
为此,您应该使用RecoverGeometryColumn-Function将正常列转换为几何列。
否则,您也可以使用AddGeometryColumn-Function
看看这个链接:
https://www.gaia-gis.it/gaia-sins/spatialite-cookbook/html/new-geom.html
此外,如果你真的想使用C#,你应该考虑Spatialite&gt; 4.2.0与SQLite的System.Data.SQLite ADO.NET提供程序。
也许你可以在这里看看这个讨论:
https://groups.google.com/forum/#!topic/spatialite-users/u2QZpQL_6ek