0

请看下面的代码

插入数据的代码

package com.example.esoftcallmanager;

import android.app.Activity;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.widget.Toast;

public class DatabaseHandler implements DatabaseConnector
{
    private SQLiteDatabase database;
    private String dbPath = "data//data//com.example.esoftcallmanager.sqllite//esoftDatabase";
    private static Context context;

    private static DatabaseHandler instance;

    private DatabaseHandler(Context context)
    {
        this.context = context;
    }

    @Override
    public void createConnection() 
    {

        try
        {
            database = context.openOrCreateDatabase("esoftDatabase", Context.MODE_PRIVATE, null);

            String createDatabaseQuery = "create table BranchNetwork(" +
                    "brID integer primary key autoincrement,"
                    +"city text,"
                    +"streetAddress text,"
                    +"phoneNumber1 text,"
                    +"phoneNumber2 text,"
                    +"email text"
                    +");";

            String dropDatabaseQuery = "drop table BranchNetwork";
            database.execSQL(createDatabaseQuery);


        }
        catch(SQLException sql)
        {
            Toast.makeText(context, sql.getMessage(), Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void closeConnection() {
        // TODO Auto-generated method stub

    }

    @Override
    public String getPhoneNumber() {
        // TODO Auto-generated method stub
        return null;
    }

    public static DatabaseHandler getInstance(Context context)
    {
        if(instance==null)
        {
            instance = new DatabaseHandler(context);
        }

        return instance;
    }

    public static DatabaseHandler getInstance()
    {
        if(instance==null)
        {
            instance = new DatabaseHandler(context);
        }

        return instance;
    }

    @Override
    public String insertData(String city, String streetAddress,String phoneNumber1, String phoneNumber2, String email) 
    {
        String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values("+city+","+streetAddress+","+phoneNumber1+","+phoneNumber2+","+email+");";

        try
        {
            database.execSQL(insertQuery);
            return "Data Successfully Inserted";
        }
        catch(Exception e)
        {
            Toast.makeText(context, "Exception: "+e.getMessage(), Toast.LENGTH_LONG).show();
            return "Data Insertion Failed";
        }

    }


}

每当我尝试插入数据时,都会出现以下错误

   02-13 23:39:40.424: E/Database(655): Failure 1 (near "@wewe": syntax error) on 0x2a6128 when preparing 'insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values(asas,asasd,3434,3434,aasa@wewe.com);'.

这里有什么问题?我该如何纠正?请帮忙!

4

3 回答 3

2

很简单,您需要将字符串用引号括起来。

"values('"+city+"','"+streetAddress+"'..."

但是,在处理原始值时,最好使用参数化查询来防止SQL 注入攻击和其他令人讨厌的事情:

db.rawQuery("insert into Foo (bar) values (?)", new String[] {city});

或使用内置方法,如SQLiteDatabase#insert()

于 2013-02-13T18:15:12.983 回答
2

尝试使用此 SQL 语句:

String insertQuery = "INSERT INTO BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') VALUES('"+city+"','"+streetAddress+"','"+phoneNumber1+"','"+phoneNumber2+"','"+email+"');";

当我使用这种 sql 语句时,我'经常遇到问题。

您也可以ContentValues用于插入/更新/删除。我更喜欢它而不是像那样编写 SQL 语句。在您的情况下,您可以这样做:

ContentValues values = new ContentValues();
values.put("city", city);
values.put("streetAddress",streetAddress);
values.put("phoneNumber1",phoneNumber1); // and ETC.
//INSERT 
sqliteDb.insert("BranchNetwork", null, values);
于 2013-02-13T18:17:05.760 回答
2

以下将不起作用:

String insertQuery = "insert into BranchNetwork ('city','streetAddress','phoneNumber1','phoneNumber2','email') values("+city+","+streetAddress+","+phoneNumber1+","+phoneNumber2+","+email+");";

您不应该创建自己的插入 SQL,而是使用ContentValues并且只使用类似的东西来插入:

db.insert(TABLENAME, null, cv);
于 2013-02-13T18:17:43.757 回答