DataBase类上的Android数据库语法错误

时间:2015-05-15 16:08:10

标签: android database android-context android-database

我试图在我的数据库中插入一些值,但我得到语法错误,我不知道为什么......任何人都可以帮助我吗? :)

数据库代码:

package com.cetabo.trackingpoint.util;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.ContactsContract;
import android.util.Log;

import java.util.Date;

/**
 * Created by VladxD on 5/16/2015.
 */
public class DataBase extends SQLiteOpenHelper {
    private static final int DATABASE_VERSION = 2;
    private static final String TABLE_NAME = "TrackedLocations";
    private static int ID;
    public static String created;
    public static String guid;
    public static String lng;
    private static String lat;
    private static String syncronized;
    private static String sync_date;
    private static String sync_status;
    private static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME + "(" + ID + " INTEGER PRIMARY KEY AUTOINCREMENT,"
            + created + " DEFAULT CURRENT_TIMESTAMP,"
            + guid + " VARCHAR(32),"
            + lng + " VARCHAR,"
            + lat + " VARCHAR,"
            + syncronized + " CHAR(1),"
            + sync_date + " DEFAULT NULL,"
            + sync_status + " CHAR(3)" + ");";

    public DataBase(Context context) {
        super(context, TABLE_NAME, null, DATABASE_VERSION);
        Log.d("Database operation", "Table has created");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABLE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public void insertInformation(DataBase db, String guid, String lon, String lat, String syncronized, Date sync_date, String sync_status) {
        SQLiteDatabase database = db.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("guid", guid);
        values.put("lon", lon);
        values.put("lat", lat);
        values.put("syncronized", syncronized);
        values.put("sync_date", String.valueOf(sync_date));
        values.put("sync_status", sync_status);
        database.insert(TABLE_NAME, null, values);
        Log.d("Database operation", "Insert on row");
        db.close();
    }


}

这是我的类,我尝试将信息插入数据库

package com.cetabo.trackingpoint;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.cetabo.trackingpoint.dummy.DummyContent;
import com.cetabo.trackingpoint.util.DataBase;
import com.cetabo.trackingpoint.util.DetectionLocation;

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A fragment representing a list of Items.
 * <p/>
 * Large screen devices (such as tablets) are supported by replacing the ListView
 * with a GridView.
 * <p/>
 * Activities containing this fragment MUST implement the {@link OnFragmentInteractionListener}
 * interface.
 */
public class PlacesFragment extends Fragment implements AbsListView.OnItemClickListener {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private OnFragmentInteractionListener mListener;

    /**
     * The fragment's ListView/GridView.
     */
    private AbsListView mListView;
    ImageView imageView;

    /**
     * The Adapter which will be used to populate the ListView/GridView with
     * Views.
     */
    private ListAdapter mAdapter;

    // TODO: Rename and change types of parameters
    public static PlacesFragment newInstance(String param1, String param2) {
        PlacesFragment fragment = new PlacesFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public PlacesFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }

        // TODO: Change Adapter to display your content
//        mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS);
    }

    String[] test = {"test", "test"};
    String text = "Central Park";
    Date data = new Date();


//    PreferenceFragment preferenceFragment;
//    ListPreference listPreference = (ListPreference) preferenceFragment.findPreference("sync_frequency");


    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_place, container, false);
        Context ctx = view.getContext();
DataBase db = new DataBase(getActivity().getApplicationContext());

        final DetectionLocation detectionLocation = new DetectionLocation(ctx);
//        CustomListAdapter adapter=new CustomListAdapter(getActivity(),test,String.valueOf(detectionLocation.getLongitude()+"  "+detectionLocation.getLatitude()),data);
//        mListView = (AbsListView) view.findViewById(android.R.id.list);
//        mListView.setAdapter(adapter);


        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ctx);
// then you use
        Integer val = Integer.valueOf(prefs.getString("sync_frequency", ""));
        String sync_status = String.valueOf(val);

        CustomListAdapter adapter = new CustomListAdapter(getActivity(), test, String.valueOf(detectionLocation.getLongitude() + "  " + detectionLocation.getLatitude()), data);
        db.insertInformation(db,"Guid",String.valueOf(detectionLocation.getLongitude()),String.valueOf(detectionLocation.getLatitude()),"Sincronizare",data, sync_status );
        mListView = (AbsListView) view.findViewById(android.R.id.list);
        mListView.setAdapter(adapter);

        Timer timer = new Timer();

        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        CustomListAdapter adapter = new CustomListAdapter(getActivity(), test, String.valueOf(detectionLocation.getLongitude() + "  " + detectionLocation.getLatitude()), data);
                        mListView = (AbsListView) view.findViewById(android.R.id.list);
                        mListView.setAdapter(adapter);
                    }
                });
            }
        }, val);
//        getActivity().runOnUiThread(new Runnable() {
//            @Override
//            public void run() {
//                CustomListAdapter adapter = new CustomListAdapter(getActivity(), test, String.valueOf(detectionLocation.getLongitude() + "  " + detectionLocation.getLatitude()), data);
//                mListView = (AbsListView) view.findViewById(android.R.id.list);
//                mListView.setAdapter(adapter);
//            }
//        });
        mListView.setOnItemClickListener(this);
        // Set OnItemClickListener so we can be notified on item clicks


        //EU/////////////
//          System.out.printf(String.valueOf(listPreference.getValue()));
        //////////////////////////

        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
//        try {
//            mListener = (OnFragmentInteractionListener) activity;
//        } catch (ClassCastException e) {
//            throw new ClassCastException(activity.toString()
//                    + " must implement OnFragmentInteractionListener");
//        }
//        SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
//        String myPref = sharedPrefs.getString("sync_frequency", "");
//        System.out.printf("Valuarea mea este: "+myPref);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (null != mListener) {
            // Notify the active callbacks interface (the activity, if the
            // fragment is attached to one) that an item has been selected.
            mListener.onFragmentInteraction(DummyContent.ITEMS.get(position).id);
        }
    }

    /**
     * The default content for this Fragment has a TextView that is shown when
     * the list is empty. If you would like to change the text, call this method
     * to supply the text it should use.
     */
    public void setEmptyText(CharSequence emptyText) {
        View emptyView = mListView.getEmptyView();

        if (emptyView instanceof TextView) {
            ((TextView) emptyView).setText(emptyText);
        }
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p/>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        public void onFragmentInteraction(String id);
    }

}

这是我的错误:

Process: com.cetabo.trackingpoint, PID: 5764
    android.database.sqlite.SQLiteException: near "0": syntax error (code 1): , while compiling: CREATE TABLE TrackedLocations(0 INTEGER PRIMARY KEY AUTOINCREMENT,null DEFAULT CURRENT_TIMESTAMP,null VARCHAR(32),null VARCHAR,null VARCHAR,null CHAR(1),null DEFAULT NULL,null CHAR(3));
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
            at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1674)
            at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
            at com.cetabo.trackingpoint.util.DataBase.onCreate(DataBase.java:42)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
            at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
            at com.cetabo.trackingpoint.util.DataBase.insertInformation(DataBase.java:51)
            at com.cetabo.trackingpoint.PlacesFragment.onCreateView(PlacesFragment.java:114)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:456)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

你知道我能做些什么才能让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

您需要为列名提供值:

public static String created;
public static String guid;
public static String lng;
private static String lat;
private static String syncronized;
private static String sync_date;
private static String sync_status;

例如,

public static String created = "created";

等等。