sqlite中的android errno

时间:2016-07-12 17:30:10

标签: java android sqlite

插入pwd= user=

时出错
  

android.database.sqlite.SQLiteException:near“table”:语法错误(代码1):,同时编译:INSERT INTO表(pwd,user)VALUES(?,?)

  

sqLiteDatabase.execSQL(“create table”+“(Tabledata.Tableinfo.user TEXT,Tabledata.Tableinfo.pwd TEXT)”);

这里有什么问题请帮帮忙。

2 个答案:

答案 0 :(得分:1)

试试这个:

// First, create database
try
{
    // Replace ... with your entities
    db.execSQL("create table myTable (id integer primary key autoincrement, latitude Text,..." );
}
catch (SQLException e)
{
    e.printStackTrace();
}

// Now to save info in database use this code
MyDatabase db = new MyDatabase(getBaseContext(), "DataBaseNAme", null, 1);
SQLiteDatabase sql = db.getWritableDatabase();
ContentValues gpsData = new ContentValues();
gpsData.put("name", ""); // name is table column name

// At last use this
long result = sql.insert("myTable", "abc", gpsData);

if (result > 0)
{
    // Toast.makeText(getBaseContext(),"data is stored", Toast.LENGTH_SHORT).show();
    Log.e("gps data", "data is store in sqlite");
    sql.close();
    // Toast.makeText(getBaseContext(),"data is saved", Toast.LENGTH_LONG).show();
}
else
{
    // Toast.makeText(getBaseContext(),"Some problem", Toast.LENGTH_SHORT).show();
    Log.e("gps data", "data is not store in sqlite, some error occur");
}

答案 1 :(得分:0)

CREATE TABLE命令缺少表的名称。

每个CREATE TABLE语句都必须指定新表的名称。

示例CREATE TABLE声明:

CREATE TABLE users
( 
  user TEXT,
  pwd  TEXT
);

使用users语句将数据插入INSERT INTO表:

INSERT INTO users (user, pwd) VALUES ('Paul', 'California');

使用SQL Helper将数据插入users表:

// Gets the data repository in write mode
SQLiteDatabase db = mDbHelper.getWritableDatabase();

// Create a new map of values, where column names are the keys
ContentValues values = new ContentValues();
values.put("user", "Paul");
values.put("pwd", "California");

// Insert the new row
db.insert("users", null, values);