Android:无法将数据插入SQLite

时间:2013-02-13 18:10:00

标签: java android eclipse sqlite

请查看以下代码

插入数据的代码

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);'.

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

3 个答案:

答案 0 :(得分:2)

很简单,你需要用引号括起你的字符串。

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

但是,在处理原始值以防止SQL injection attacks和其他令人讨厌的事情时,通常最好使用参数化查询:

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

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

答案 1 :(得分: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);

答案 2 :(得分:2)

以下内容不起作用:

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

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

db.insert(TABLENAME, null, cv);